Cakephp: how to pass a value from default.ctp to a view
I want to pass a value from default.ctp to a view and I trie开发者_如何学God just assigning a php variable as well as
$this->set('myvariable','value');
but with no success.
Views are rendered first and then embedded into the layout. You can pass variables from views to the layout, but not the other way around.
As the other answer has stated, the view is rendered before the layout, meaning that you can't pass information in that direction. Whatever information it is that you want to pass to default.ctp
will need to come from a controller. If you're reluctant to do that because it is shared between multiple controllers, you might want to consider putting it in a component, or alternatively inside your application's app_controller.php
in the beforeFilter
method, so that it is always available for default.ctp
.
精彩评论