Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Redirect в initialize и beforeExecuteRoute не работает/Redirect to initalize and beforeExecuteRoute not working

Столкнулся с неожиданным поведением фреймворка. $this->response->redirect('login') отлично работает в action-ах, но не работает в initialize и beforeExecuteRoute. С чем это связано, подскажите plz. И как сделать редирект, не используя header?


Faced with a unexpected framework behavior. $this->response->redirect('login') works fine in actions, but it does not work in initialize and beforeExecuteRoute, What is the reason? Tell me please, and how to make a redirection without headers?



85.3k
Accepted
answer

You can make redirections in beforeExecuteRoute this way:

public function beforeExecuteRoute($dispatcher)
{
        $dispatcher->setReturnedValue($this->response->redirect('/session'));
        return false;
}

Or you can use a 'forward' if you don't want to use headers: http://docs.phalconphp.com/es/latest/reference/dispatching.html#forwarding-to-other-actions



6.8k

Thank you. What accounts for the difference in treatment? And that should work in initialize? Currently in initialize does not work one way.



85.3k

Initialize is not intended to make redirections, you should use 'initialize' just to initialize the controller environment the first time it is used.