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?



3.7k
edited Nov '15

This works:

$this->response->redirect('http://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.phalconphp.com/en/latest/reference/dispatching.html#dispatch-loop-events