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

afterExecuteRoute does not get called in Micro app

Hi,

I'm having some problems with my Micro app. The documentation states:

"An instantiated controller automatically acts as a listener for dispatch events, so you can implement methods as callbacks:"

However, when I implement that function into my controller, it does not get called. Is this a limitation of the micro app or am I doing something wrong?

Controller function:

    /**
     * Changed output to valid JSON
     * @param \Phalcon\Mvc\Dispatcher $dispatcher
     */
    public function afterExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher)
    {
        echo "test";
        die();

        $this->response->setContentType('application/json', 'UTF-8');

        $data = $dispatcher->getReturnedValue();

        if (is_array($data))
        {
            $this->response->setJsonContent($data);
        }
        else
        {
            $this->response->setStatusCode(500, "Internal Server Error");
            $this->response->setJsonContent(array("status" => "ERROR"));
        }

        $this->response->send();
    }

To setup my dispatcher in the Micro bootstrap file:

    //Setup dispatcher
    $di->set('dispatcher', function(){

        //Create an event manager
        $eventsManager = new EventsManager();

        $dispatcher = new MvcDispatcher();

        //Bind the eventsManager to the view component
        $dispatcher->setEventsManager($eventsManager);

        return $dispatcher;

    }, true);


98.9k
Accepted
answer

Micro Applications does not have a dispatcher, you may want to check the micro-apps middleware: https://docs.phalcon.io/en/latest/reference/micro.html#middleware-events