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

Redirect / Forward

How can i Redirect or Forward to previous controller dynamically ?

Ex:

Send a request from : index/dashboard , to : request/setlanguage ;

So request/setlanguage will redirect back to where it has been requested.



51.2k
Accepted
answer

One way would be using

$ref = $this->request->getHTTPReferer();

return $this->response->redirect($ref);

The disadvantage of this, would be that you need to ensure that the referer is your host.

Another way is to use dispatcher:

$controller = $this->dispatcher->getPreviousControllerName();
$action = $this->dispatcher->getPreviousActionName();

return $this->response->redirect("$controller/$action");