I want to define an array which I can use in AppController
and in the default layout.
How can I do this in CakePHP?
Many applications have small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout. CakePHP can help you repeat parts of your website that need to be reused. These reusable parts are called Elements.
$this->set('arr', array('one', 'two'));
// Accessible in controller as
$this->viewVars['arr'];
// Accessible in view/layout as
echo $arr;
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With