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

Should the URL change in the browser?

Hi there,

I have the following code:

class ControllerBase extends \Phalcon\Mvc\Controller
{
    /**
     * Kicks in before a route is executed
     *
     * @param $dispatcher
     * @return bool
     */
    public function beforeExecuteRoute($dispatcher)
    {
        if (!$this->session->has('user')) {
            if ($dispatcher->getControllerName() == 'login' && $dispatcher->getActionName() == 'new'
                || $dispatcher->getActionName() == 'create') {
                return;
            }
            $this->dispatcher->forward(array(
                'controller' => 'login',
                'action' => 'new'
            ));
            return false;
        }
    }
}

When the above code is triggered, I would expect the URL in the browser to say something like https://10.10.22.35/login/new but instead, it says https://10.10.22.35/host_ui/index

Obviously, based on the above beforeExecuteRoute code, when the initial page is loaded (https://10.10.22.35/host_ui/index), there isn't a session item called 'user' and therefore it correctly forwards to login/new. However, I expect the URL in the browser to change? Am I misunderstanding something here? Is my beforeExecuteRoute wrong?

Any advice much appreciated.



13.7k
Accepted
answer

In my application I use $this->response->redirect("login/new"); The forward method keeps the user at the some URL, but renders your "forward"-controller

forward only forwards the request internally to your application. $this->response->redirect() actually forwards the user.



38.8k

Thanks guys. That explains it nicely :)