Reputation: 1041
I want to define an array which I can use in AppController
and in the default layout.
How can I do this in CakePHP?
Upvotes: 5
Views: 12125
Reputation: 539
If you set any variable in AppController beforeRender() function,and set that variable,then you can access easily that variable anywhere in the view files
function beforeRender() {
parent::beforeRender();
$sample_arr = array("abc","xyz");
$this->set('sample_arr',$sample_arr);
}
In your Layout File Just Print that Array like
print_r($sample_arr);
Upvotes: 2
Reputation: 8100
$this->set('arr', array('one', 'two'));
// Accessible in controller as
$this->viewVars['arr'];
// Accessible in view/layout as
echo $arr;
Upvotes: 26
Reputation: 1056
from here:
// First you pass data from the controller:
$this->set('color', 'pink');
// Then, in the view, you can utilize the data:
?>
You have selected <?php echo $color; ?> icing for the cake.
So for your situation:
$arr = array('stuff', 'more stuff');
$this->set('arr', $arr);
// then in the layout
print_r($arr);
Upvotes: 0