Nitish
Nitish

Reputation: 2763

Pass more than one variable from controller to view in cakephp

I am new to cakephp. I need to send two variables to the view. In codeigniter its easy

$data['var1'] = 'One';
$data['var2'] = 'Two';
$this->load->view('myview',$data);

Now in Cakephp, I have a controller called function names search() in which I am sending an associative array to view.

$gal_providers = $this->GalProvider->getListByCategory(3,$location_id,false,9);
$this->set("gal_providers",$gal_providers);

But I need to send the variable $location_id too to the view. How can I send it ?

I read the article Using set() and compact() together, but I did not get the solution I was looking for.

Upvotes: 1

Views: 6562

Answers (1)

floriank
floriank

Reputation: 25698

The blog tutorial describes very well how to set data to the view. I recommend you to do the tutorial first, it gives you all you need to do your first steps.

You can set variables using $this->set() in your controller:

$this->set('first', 'second');
$this->set(array('foo' => 'bar', 'something' => 'else'));

The first will make the variable $first with the value second available in the view. The second will make $foo with value bar and $something with value else available in the view.

Controll::set() is setting the data to the view instances viewVars property. When the view is rendered it turns them into variables available in the view templates.

And do yourself and other people who look at your code a favour and follow the conventions and coding standards.

Upvotes: 6

Related Questions