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.

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 http://10.10.22.35/login/new but instead, it says http://10.10.22.35/host%5ui/index

Obviously, based on the above beforeExecuteRoute code, when the initial page is loaded (http://10.10.22.35/host%5ui/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.



7.6k
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.



23.9k

Thanks guys. That explains it nicely :)