Silex 2 addListener trigger error

219 Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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