We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Render another view and pass variables to it

Hello everyone. Could somebody please explain me how to render another view from a controller and pass data to it. Another question. I am wondering if I do something like

$this->response->redirect('');

in a controller. Is there a way to somehow pass data to that view that I am redirecting a user to? Is it possible to do only with session or any other ways are possible.



12.2k

You could use the dispatcher to forward control onto another action

https://docs.phalcon.io/en/latest/reference/controllers.html#dispatch-loop

You could pick a new view https://docs.phalcon.io/en/latest/reference/views.html#picking-views and set the variables as you would normally

https://docs.phalcon.io/en/latest/reference/views.html#transfer-values-from-the-controller-to-views

This won't redirect the url though. However if you are intent on redirecting, I would suggest storing the vars in a session bag and then retrieving them on the new controller (checking if they are set), Once these have been assigned to new variables in the view, delete them from the session.

More info at https://docs.phalcon.io/en/latest/reference/session.html