4

I'm trying to add an event listener/dispatcher to one of my controller action. It's goal is to add default data to my entity before persisting it in my database.

Here is what I've got so far.

app.php

$app['dispatcher']->addListener('my_event_name', function (Event $event) {
    // do something;
});

It ends up with the following error :

( ! ) InvalidArgumentException: Identifier "dispatcher" does not contain an object definition. in /var/www/site/vendor/pimple/pimple/src/Pimple/Container.php on line 233

Tim
  • 1,238
  • 1
  • 14
  • 24
  • Can you post your whole controller code? From the error I would say that the ```$app``` container does not have the dispatcher identifier configured, which is [weird](https://github.com/silexphp/Silex/blob/master/src/Silex/Provider/HttpKernelServiceProvider.php#L76). – mTorres Jan 05 '17 at 16:26

1 Answers1

1

Check out this thread. Looks like it has to do with a precise order in which you call and initialize.

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

https://github.com/silexphp/Silex-WebProfiler/issues/70#issuecomment-170399805

Serg Chernata
  • 12,280
  • 6
  • 32
  • 50