Phalcon redirection and forwarding

Ian Bytchek picture Ian Bytchek · Jan 23, 2014 · Viewed 28k times · Source

Do I understand correctly that after doing $this->dispatcher->forward() or $this->response->redirect() I need to manually ensure that the rest of the code does't get executed? Like below, or am I missing something?

public function signinAction()
{
    if ($this->isUserAuthenticated())
    {
        $this->response->redirect('/profile');
        return;
    }

    // Stuff if he isn't authenticated…
}

Answer

Ian Bytchek picture Ian Bytchek · Oct 14, 2014

After almost a year of working on a hardcore project that uses Phalcon beyond its capacity, I wanted to clarify a few things and answer my own question. To understand how to properly do redirects and forwards you need to understand a little about how the Dispatcher::dispatch method works.

Take a look at the code here, though it's all C mumbo-jumbo to most of us, its really well written and documented. In the nutshell this is what it does:

  1. The dispatcher enters the while loop until the _finished property becomes true or it discovers a recursion.
  2. Inside the loop, it immediately sets that property to true, so when it starts the next iteration it will automatically break.
  3. It then gets the controller / action information, which are originally supplied by the router in the application, and does various checks. Before and after that it also completes a lot of event-related business.
  4. Finally it calls the action method in the controller and updates the _returnedValue property with (guess what!) the returned value.
  5. If during the action call you call Dispatcher::forward method, it will update the _finished property back to false, which will allow the while loop to continue from the step 2 of this list.

So, after you do redirect or forward, you need to ensure that you code doesn't get executed only if that is part of the expected logic. In other words you don't have to return the result of return $this->response->redirect or return $this->dispatcher->forward.

Doing the last might seem convenient, but not very correct and might lead to problems. In 99.9% cases your controller should not return anything. The exception would be when you actually know what you are doing and want to change the behaviour of the rendering process in your application by returning the response object. On top of that your IDE might complain about inconsistent return statements.

To finalise, the correct way to redirect from within the controller:

// Calling redirect only sets the 30X response status. You also should
// disable the view to prevent the unnecessary rendering.

$this->response->redirect('/profile');
$this->view->disable();

// If you are in the middle of something, you probably don't want 
// the rest of the code running.

return; 

And to forward:

$this->dispatcher->forward(['action' => 'profile']);

// Again, exit if you don't need the rest of the logic.

return;