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

Ending action execution

I am currently using a forward404 or forward404Unless with the following code:


    protected function forward404(){
      $this->dispatcher->forward(
        array(
          'controller' => 'error',
          'action' => 'error404'
        )
      );
    }

    protected function forward404Unless($what){
      if(!$what) $this->forward404();
    }

The problem that i have is that i need to stop the execution of the current action and pass to the new one (404):

public function addAction(){

    $this->forward404Unless($this->request->get('user_id'));
    $this->forward404Unless($user = \Zero\Models\User::findFirstByUserId($this->request->get('user_id')));

Any suggestions on how to stop the execution of any action?

edited Mar '14

It seems like it works with a custom Exception like this:

protected function forward404(){
      throw new \Zero\MVC\Exception\Http404Exception();
    }
 $eventsManager->attach('dispatch:beforeException', function(\Phalcon\Events\Event $event, $dispatcher, \Exception $exception) {
    $class = get_class($exception);
    if($class == 'Zero\MVC\Exception\Http404Exception') {
      $event->stop();
      $dispatcher->forward(
        array(
          'controller' => 'error',
          'action'     => 'error404'
        )
      );
return false;
    }
  });


98.9k

You can also do a return to stop the current action execution:

protected function forward404Unless($what){
      if(!$what) return $this->forward404();
}