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.

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



31.6k
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: http://pastebin.com/BvFUx0KL dependencies.php: http://pastebin.com/u4rCWyhY



5.0k
Accepted
answer

Got it! The modules.php has overwritten it.

Thanks