config a listener to execute only in one bundle symfony 2

symfony

Solution

You're thinking about this incorrectly. You can't configure a listener to execute for a bundle. In practice a listener just waits for its event to be fired -it's the event that defines when a listener gets called. What you really want to achieve is to fire an event before each action in your controller(s).

You could do this buy listening for the kernel.Controller event then doing something like this:

$controller = $event->getController();
if ($controller instanceof mybundlecontroller) {
    // fire custom event e.g. My.db.lookup
}
$event->setController($controller)

You can then have a separate listener that fires in this case.

See docs: http://symfony.com/doc/current/book/internals.html#kernel-controller-event

Problem

We need to configure a listener to check some registers in a db but we need to do this check action only in one bundle for every actions in it. We don't want to call a function everytime we write a function in the bundle so we have thought to do a listener. Does Symfony allow to configure a listener only in one bundle? Thanks.

Original source