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?


You can make redirections in beforeExecuteRoute this way:

public function beforeExecuteRoute($dispatcher)
        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


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


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