From 36407e0c1c10c571049d0ebfecc4c7bc6b5c336f Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 13 Jun 2014 19:01:18 +0200 Subject: [PATCH] describe the usage of the RegisterListenersPass --- components/event_dispatcher/introduction.rst | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/components/event_dispatcher/introduction.rst b/components/event_dispatcher/introduction.rst index 6d8a5f102f3..7e4e248c021 100644 --- a/components/event_dispatcher/introduction.rst +++ b/components/event_dispatcher/introduction.rst @@ -205,6 +205,47 @@ instance of ``Symfony\Component\HttpKernel\Event\FilterResponseEvent``:: // ... } +.. sidebar:: Registering Event Listeners in the Service Container + + When you are using the + :doc:`DependencyInjection component `, + you can use the + :class:`Symfony\\Component\\HttpKernel\\DependencyInjection\\RegisterListenersPass` + from the HttpKernel component to tag services as event listeners:: + + use Symfony\Component\DependencyInjection\ContainerBuilder; + use Symfony\Component\DependencyInjection\Definition; + use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; + use Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass; + + $containerBuilder = new ContainerBuilder(new ParameterBag()); + $containerBuilder->addCompilerPass(new RegisterListenersPass()); + + // register the event dispatcher service + $containerBuilder->register( + 'event_dispatcher', + 'Symfony\Component\EventDispatcher\EventDispatcher' + ); + + // register your event listener service + $listener = new Definition('AcmeListener'); + $listener->addTag('kernel.event_listener', array( + 'event' => 'foo.action', + 'method' => 'onFooAction', + )); + $containerBuilder->setDefinition('listener_service_id', $listener); + + // register an event subscriber + $subscriber = new Definition('AcmeSubscriber'); + $subscriber->addTag('kernel.event_subscriber'); + $containerBuilder->setDefinition('subscriber_service_id', $subscriber); + + By default, the listeners pass assumes that the event dispatcher's service + id is ``event_dispatcher``, that event listeners are tagged with the + ``kernel.event_listener`` tag and that event subscribers are tagged with + the ``kernel.event_subscriber`` tag. You can change these default values + by passing custom values to the constructor of ``RegisterListenersPass``. + .. _event_dispatcher-closures-as-listeners: .. index::