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

Registered dispatcher does not get called

Hi,

I've a working REST app with routing etc. Actually it responses

{"data":true}

Just fine so far. Now I like to add a plugin to the dispatch loop so I added it to the DI but nothing happens. Next I tested if it gets called at all, and unfortunatelly that's not the case.

TestCode:

/** Register default dependencies */
$dependencyInjector = new Phalcon\DI\FactoryDefault(); 

/** Register dispatcher */
$dependencyInjector->set(
    'dispatcher', function () use ($dependencyInjector) {
die('hier');
);

$application = new Application($dependencyInjector);
echo $application->handle()->getContent();

If I run the code, the response is the same as above, like the dispatcher injection doesn't exist anyway. Some hints what I'm doing wrong?

Best, Thorsten



51.2k
edited Mar '14

Check:

    $di['dispatcher'] = function () use ($di) {
            $eventsManager = $di->getShared('eventsManager');
            $eventsManager->attach('dispatch', new \Path\To\Your\Implementation($di));

            $dispatcher = new Dispatcher();
            $dispatcher->setEventsManager($eventsManager);

            return $dispatcher;
    };

Then in your :

<?php
namespace Path\To\Your;

use Phalcon\Events\Event,
use Phalcon\Mvc\Dispatcher;

class Implementation
{
    /**
     * beforeDispatchLoop
     *
     * @param  Event                           $event
     * @param  Dispatcher                      $dispatcher
     * @return \Phalcon\Http\ResponseInterface
     */
    public function beforeDispatchLoop(Event $event, Dispatcher $dispatcher)
    {
        // Here you can call die() if you want. 
        // This is one of the correct way to do it
    }

Hi Calin,

thats exactly what I did, except the array notation, but now, I tried that also. After that did not work I tried it with placing a breakpoint within $di['dispatcher'] ... but it never get called. Next step I added the die('called') but the output is always the same, like the dispatcher injection doesn't exist anyway.

index.php: https://pastebin.com/BvFUx0KL dependencies.php: https://pastebin.com/u4rCWyhY



6.9k
Accepted
answer

Got it! The modules.php has overwritten it.

Thanks