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

Preempting the dispathcer

Hello,

I have an afterExecuteRoute route set up in my base Controller class. To redirect, I use:

$this->response->redirect('login');
$this->view->disable();
return;

I track XDebug and notice afterExecuteRoute still executes after the redirect code. How do I prevent this method from firing when I redirect?



8.7k
edited Nov '15

This works:

$this->response->redirect('https://site.com/login', true);

Is there a better way?

UPDATE: XDebug still shows afterExecute firing. Now using a $stopDispatcher instance variable. :(

edited Nov '15

afterExecuteRoute cannot stop the dispatch loop, you should use beforeExecuteRoute.

Redirect will generate a full response with 30x (302 if i remember correctly) code, so view->disable is not really needed.

You may also consider using dispatcher->forward to render the login page in the same request, thus saving an extra redirect. The URL will stay the same though.

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