Matt Drake
Matt Drake

Reputation: 57

Phalcon PHP passing parameters to views that belongs to another controller

This is a real newbie question, but I have not used PHP and Phalcon very long and I am sort of learning by studying examples, reading on internet and a bit of trial and error.

One thing that I got stuck on is how to pass variables to views that belongs to another controller. If I want to pass a variable to a view in the same controller, lets call it showRoomController, then I simply use.

$this->view->setVar("id", $cars->id);

However, if I want to open the cars view from catalogueController, but from a page that belongs to showRoomController I use this:

return $this->forward("catalogue/cars");

How can I pass the cars id variable in the second example? Or do I need to use global variables?

I apologize if this is a very basic question that I probably should know.

Upvotes: 2

Views: 2703

Answers (2)

Ian Bytchek
Ian Bytchek

Reputation: 9075

By default your view is a shared service in the DI. You can simply set parameters as you do in one controller, and when it did forward to another all of those parameters would still be there.

When you do $this->view in your controller it uses a magic method to get the view service from the DI, so if you do that in both controllers you will be referencing the same view.

Upvotes: 0

Phantom
Phantom

Reputation: 1700

Dispatcher's forward() method accepts params as well:

$this->dispatcher->forward(array(
    "controller" => "myController",
    "action"     => "myAction",
    "params"     => array('name' => 'hello', 'surname' => 'world')
));

Upvotes: 2

Related Questions