@@ -22,7 +22,7 @@ answer.
22
22
Consider the real-world example where you want to provide a plugin system
23
23
for your project. A plugin should be able to add methods, or do something
24
24
before or after a method is executed, without interfering with other plugins.
25
- This is not an easy problem to solve with single inheritance, and even if
25
+ This is not an easy problem to solve with single inheritance, and even if
26
26
multiple inheritance was possible with PHP, it comes with its own drawbacks.
27
27
28
28
The Symfony EventDispatcher component implements the `Mediator `_ pattern
@@ -187,33 +187,28 @@ event. In many cases, a special event subclass is passed with extra
187
187
information. You can check the documentation or implementation of each event to
188
188
determine which instance is passed.
189
189
190
- .. sidebar :: Registering Event Listeners in the Service Container
190
+ .. sidebar :: Registering Event Listeners and Subscribers in the Service Container
191
191
192
- When you are using the
193
- :class: `Symfony\\ Component\\ EventDispatcher\\ ContainerAwareEventDispatcher `
194
- and the
195
- :doc: `DependencyInjection component </components/dependency_injection >`,
196
- you can use the
197
- :class: `Symfony\\ Component\\ EventDispatcher\\ DependencyInjection\\ RegisterListenersPass `
198
- to tag services as event listeners::
192
+ Registering service definitions and tagging them with the
193
+ ``kernel.event_listener `` and ``kernel.event_subscriber `` tags is not enough
194
+ to enable the event listeners and event subscribers. You must also register
195
+ a compiler pass called ``RegisterListenersPass() `` in the container builder::
199
196
200
197
use Symfony\Component\DependencyInjection\ContainerBuilder;
201
198
use Symfony\Component\DependencyInjection\Definition;
202
199
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
203
200
use Symfony\Component\DependencyInjection\Reference;
204
- use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher ;
201
+ use Symfony\Component\EventDispatcher\EventDispatcher ;
205
202
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
206
203
207
204
$containerBuilder = new ContainerBuilder(new ParameterBag());
205
+ // register the compiler pass that handles the 'kernel.event_listener'
206
+ // and 'kernel.event_subscriber' service tags
208
207
$containerBuilder->addCompilerPass(new RegisterListenersPass());
209
208
210
- // register the event dispatcher service
211
- $containerBuilder->setDefinition('event_dispatcher', new Definition(
212
- ContainerAwareEventDispatcher::class,
213
- array(new Reference('service_container'))
214
- ));
209
+ $containerBuilder->register('event_dispatcher', EventDispatcher::class);
215
210
216
- // register your event listener service
211
+ // register an event listener
217
212
$listener = new Definition(\AcmeListener::class);
218
213
$listener->addTag('kernel.event_listener', array(
219
214
'event' => 'foo.action',
@@ -441,7 +436,7 @@ EventDispatcher Aware Events and Listeners
441
436
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
442
437
443
438
The ``EventDispatcher `` always passes the dispatched event, the event's
444
- name and a reference to itself to the listeners. This can lead to some advanced
439
+ name and a reference to itself to the listeners. This can lead to some advanced
445
440
applications of the ``EventDispatcher `` including dispatching other events inside
446
441
listeners, chaining events or even lazy loading listeners into the dispatcher object.
447
442
0 commit comments