Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp - one variable accessible in view and controller

I want to define an array which I can use in AppController and in the default layout.

How can I do this in CakePHP?

like image 332
Piotr Łużecki Avatar asked Mar 01 '13 23:03

Piotr Łużecki


People also ask

What is element 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.


2 Answers

$this->set('arr', array('one', 'two'));

// Accessible in controller as 
$this->viewVars['arr'];

// Accessible in view/layout as
echo $arr;
like image 96
ADmad Avatar answered Oct 18 '22 03:10

ADmad


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);
like image 45
Hareesh Avatar answered Oct 18 '22 02:10

Hareesh