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

Performing a route redirection in PhalconPHP

After doing some searching around the PhalconPHP documentation, forums, and Google it became apparent that one of two things were happening here: either no one that is using PhalconPHP has had to do rerouting or there is a way and it just hasn’t been shared with the rest.

Either way, after digging through the PhalconPHP API we found something that could help us with what we wanted. Therefore we created a solution to what we needed. It might not be the prettiest or best one out there, but it worked and took care of it. https://phalcon.io/performing-a-route-redirection-in-phalconphp



17.5k

Here is my controller function for activating users which redirects depending on the message from the model (make sure you use Phalcon\Http\Response):

    public function activateAction() {
        $response = new Response();

        $user = User::findFirstByActiveHash($this->dispatcher->getParam("hash"));

        if (empty($user)) {
            $response->redirect("/activate-failure",false);
            $response->send();
        } else if ($user->activateAccount()) {
            $response->redirect("/activate-success",false);
            $response->send();
        } else {
            $messages = $user->getMessages();
            foreach($messages as $message) {
                switch($message) {
                    case "activate-failure":
                        $response->redirect("/activate-failure",false);
                        $response->send();
                        break;
                    case "user-active":
                        $response->redirect("https://www.the-dealio.com/login",false);
                        $response->send();
                        break;
                }
            }
        }
    }