Silex-WebProfiler icon indicating copy to clipboard operation
Silex-WebProfiler copied to clipboard

Silex 2.0 and 'Identifier "dispatcher" does not contain an object definition.'

Open J7mbo opened this issue 9 years ago • 6 comments

Note: I'm using silex 2.0.x-dev#c207787 and WebProfiler 2.0.x-dev.

Registering the WebProfilerServiceProvider with the following code:

    $app->register(new WebProfilerServiceProvider, [
        'profiler.cache_dir'    => __DIR__ .  $cacheDir,
        'profiler.mount_prefix' => $mountPrefix
    ]);

Throws the following exception:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Identifier "dispatcher" does not contain an object definition.' in /vagrant/vendor/pimple/pimple/src/Pimple/Container.php on line 232 ( ! ) InvalidArgumentException: Identifier "dispatcher" does not contain an object definition. in /vagrant/vendor/pimple/pimple/src/Pimple/Container.php on line 232

It seems that Container::extend() is throwing the exception because the value "dispatcher" (the EventDispatcher object) doesn't contain the __invoke() method:

screen shot 2015-09-05 at 18 36 28 screen shot 2015-09-05 at 18 37 10

J7mbo avatar Sep 05 '15 17:09 J7mbo

I've had a similar issue and noticed that the sequence you add things to the container matters. For example, once I did $app['twig']->addGlobal('foo', 'bar');, everything added after that failed. So by registering the WebProfilerServiceProvider earlier, fixed it for me. Not too elegant, but at least I could continue. Just letting you know of a possible solution.

xiaohutai avatar Sep 11 '15 08:09 xiaohutai

Hmm, thanks for the suggestion @xiaohutai. The only reason I registered it last is because the documentation states, in italics, I might add:

Make sure to register all other required or used service providers before WebProfilerServiceProvider.

I'll give switching things around a go anyway and see how that goes.

J7mbo avatar Sep 11 '15 09:09 J7mbo

I've also had this issue although not specifically with the web profiler but other service providers. I've also found that moving things around can sometimes help. It just sounds like something that is fundamentally broken with Silex and needs to be fixed.

kabudu avatar Dec 26 '15 21:12 kabudu

Same here, it seams to happen if $app['dispatcher'] was already evaluated with a $this['dispatcher']->addListener(...) or something else... be sure to put those calls in the boot() method => it s not a bug

quazardous avatar Jan 10 '16 22:01 quazardous

Confirm that moving any dispatcher related code like addListener to the end fixes the problem

sinasalek avatar Jun 19 '16 08:06 sinasalek

To avoid this bug, I either add listener (or subscribers) in boot method, or use before silex middleware:

$this->before(function () {
    $this['dispatcher']->addListener($eventName, $callback);
});

alcalyn avatar Jun 19 '16 12:06 alcalyn