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.

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);


85.4k
Accepted
answer

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