Piotr Łużecki
Piotr Łużecki

Reputation: 1041

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?

Upvotes: 5

Views: 12125

Answers (3)

Hareesh
Hareesh

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

ADmad
ADmad

Reputation: 8100

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

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

// Accessible in view/layout as
echo $arr;

Upvotes: 26

newshorts
newshorts

Reputation: 1056

from here:

cakephp set function

// 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

Related Questions