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 в 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?



98.9k
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: https://docs.phalcon.io/es/latest/reference/dispatching.html#forwarding-to-other-actions



10.2k

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



98.9k

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