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

Phalcon 1.2.6 beforeExecuteRoute interrupt

Hi everyone! I have a question: how can I stop/interrupt beforeExecuteRoute handling? It is always continue handling and run the action. Example:

    //Base controller for some actions
    class AController extends BaseController
    {
        public function beforeExecuteRoute($dispatcher)
        {
            $action = $this->dispatcher->getActionName();
            if ($action !== 'token') {
                $this->token = $this->dispatcher->getParam('token');
                if (!$this->token || !$this->isAvailableToken($this->token)) {
                    $this->view->disable();
                    $this->response->setStatusCode(401, "Unauthorized");
                    $this->response->setContentType('application/json', 'UTF-8');
                    $this->response->setJsonContent(array(
                        'status'  => 401,
                        'message' => 'Your token in not available',
                        'token'   => '',
                    ));
                    return $this->response->send(); //does not work, continue to run action

                    //return false; //does not work, continue to run action
                    //return $this->response->redirect('api/auth'); //work but It is not my case

                    //return $this->response->send();
                    //exit; //work, but is not best solution
                }
            }
            return true;
        }
    }

    class WController extends AController {
        public function beforeExecuteRoute($dispatcher) {
            parent::beforeExecuteRoute($dispatcher);
            //I do not want to run this, parent beforeExecuteRoute have to be interrupted
            /*
            * some logic here
            */
        }

        public function indexAction()
        {
            echo 'it will be called';
        }
    }
edited Mar '14

Hi, try this:

class AController extends BaseController 
{
    public function onConstruct() //onConstruct is call even if action method isn't exist
    {
        $action = $this->dispatcher->getActionName();
        if ($action !== 'token') 
        {
            // whatever
        }
    }
}
edited Mar '14

My actions do exist, and I have a handler for notFound and this logic works perfect. I have an issue with: after beforeExecuteRoute() processed it call action anyway. I can do exit; but it is not a good way, then if I try to use return $this->response->send(); or return $this->response->redirect('api/auth'); it does not work. I made a more elegant solution:

    //parent class
    class ParentController extends Controller
    {
        public function beforeExecuteRoute($dispatcher)
        {
            //do check for token
            if ($token) {
                return true;
            }
            return false;
        }
    }

    class ChildController extends ParentController
    {
        public function beforeExecuteRoute($dispatcher)
        {
            $before = parent::beforeExecuteRoute($dispatcher);
            if ($before) {
                //do some logic
            }
            return $before;
        }
    }

This solution takes us control for each nested level action calling.



40.8k
Accepted
answer

onConstruct() is executed everytime before initialize() is called, if action exist or not.
you can use parent::onConstruct() too

Oh, now I understand, thanks for Your response!