The EventDispatcher Component

The EventDispatcher Component

The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them.

Introduction

Object-oriented code has gone a long way to ensuring code extensibility. By creating classes that have well-defined responsibilities, your code becomes more flexible and a developer can extend them with subclasses to modify their behaviors. But if they want to share the changes with other developers who have also made their own subclasses, code inheritance is no longer the answer.

Consider the real-world example where you want to provide a plugin system for your project. A plugin should be able to add methods, or do something before or after a method is executed, without interfering with other plugins. This is not an easy problem to solve with single inheritance, and even if multiple inheritance was possible with PHP, it comes with its own drawbacks.

The Symfony EventDispatcher component implements the Mediator and Observer design patterns to make all these things possible and to make your projects truly extensible.

Take an example from the HttpKernel component. Once a Response object has been created, it may be useful to allow other elements in the system to modify it (e.g. add some cache headers) before it’s actually used. To make this possible, the Symfony kernel throws an event - kernel.response. Here’s how it works:

  • A listener (PHP object) tells a central dispatcher object that it wants to listen to the kernel.response event;
  • At some point, the Symfony kernel tells the dispatcher object to dispatch the kernel.response event, passing with it an Event object that has access to the Response object;
  • The dispatcher notifies (i.e. calls a method on) all listeners of the kernel.response event, allowing each of them to make modifications to the Response object.

Installation

  1. $ composer require symfony/event-dispatcher

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Usage

See also

This article explains how to use the EventDispatcher features as an independent component in any PHP application. Read the Events and Event Listeners article to learn about how to use it in Symfony applications.

Events

When an event is dispatched, it’s identified by a unique name (e.g. kernel.response), which any number of listeners might be listening to. An Symfony\Contracts\EventDispatcher\Event instance is also created and passed to all of the listeners. As you’ll see later, the Event object itself often contains data about the event being dispatched.

Naming Conventions

The unique event name can be any string, but optionally follows a few naming conventions:

  • Use only lowercase letters, numbers, dots (.) and underscores (_);
  • Prefix names with a namespace followed by a dot (e.g. order.*, user.*);
  • End names with a verb that indicates what action has been taken (e.g. order.placed).

Event Names and Event Objects

When the dispatcher notifies listeners, it passes an actual Event object to those listeners. The base Event class contains a method for stopping event propagation, but not much else.

See also

Read “The Generic Event Object” for more information about this base event object.

Often times, data about a specific event needs to be passed along with the Event object so that the listeners have the needed information. In such case, a special subclass that has additional methods for retrieving and overriding information can be passed when dispatching an event. For example, the kernel.response event uses a Symfony\Component\HttpKernel\Event\ResponseEvent, which contains methods to get and even replace the Response object.

The Dispatcher

The dispatcher is the central object of the event dispatcher system. In general, a single dispatcher is created, which maintains a registry of listeners. When an event is dispatched via the dispatcher, it notifies all listeners registered with that event:

  1. use Symfony\Component\EventDispatcher\EventDispatcher;
  2. $dispatcher = new EventDispatcher();

Connecting Listeners

To take advantage of an existing event, you need to connect a listener to the dispatcher so that it can be notified when the event is dispatched. A call to the dispatcher’s `addListener() method associates any valid PHP callable to an event:

  1. $listener = new AcmeListener();
  2. $dispatcher->addListener('acme.foo.action', [$listener, 'onFooAction']);

The `addListener() method takes up to three arguments:

  1. The event name (string) that this listener wants to listen to;
  2. A PHP callable that will be executed when the specified event is dispatched;
  3. An optional priority, defined as a positive or negative integer (defaults to 0). The higher the number, the earlier the listener is called. If two listeners have the same priority, they are executed in the order that they were added to the dispatcher.

Note

A PHP callable is a PHP variable that can be used by the call_user_func() function and returnstruewhen passed to theis_callable() function. It can be a \Closure instance, an object implementing an `__invoke() method (which is what closures are in fact), a string representing a function or an array representing an object method or a class method.

So far, you’ve seen how PHP objects can be registered as listeners. You can also register PHP Closures as event listeners:

  1. use Symfony\Contracts\EventDispatcher\Event;
  2. $dispatcher->addListener('acme.foo.action', function (Event $event) {
  3. // will be executed when the acme.foo.action event is dispatched
  4. });

Once a listener is registered with the dispatcher, it waits until the event is notified. In the above example, when the acme.foo.action event is dispatched, the dispatcher calls the AcmeListener::onFooAction() method and passes theEvent` object as the single argument:

  1. use Symfony\Contracts\EventDispatcher\Event;
  2. class AcmeListener
  3. {
  4. // ...
  5. public function onFooAction(Event $event)
  6. {
  7. // ... do something
  8. }
  9. }

The $event argument is the event object that was passed when dispatching the event. In many cases, a special event subclass is passed with extra information. You can check the documentation or implementation of each event to determine which instance is passed.

Registering Event Listeners and Subscribers in the Service Container

Registering service definitions and tagging them with the kernel.event_listener and kernel.event_subscriber tags is not enough to enable the event listeners and event subscribers. You must also register a compiler pass called `RegisterListenersPass() in the container builder:

  1. use Symfony\Component\DependencyInjection\ContainerBuilder;
  2. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  3. use Symfony\Component\DependencyInjection\Reference;
  4. use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
  5. use Symfony\Component\EventDispatcher\EventDispatcher;
  6. $containerBuilder = new ContainerBuilder(new ParameterBag());
  7. // register the compiler pass that handles the 'kernel.event_listener'
  8. // and 'kernel.event_subscriber' service tags
  9. $containerBuilder->addCompilerPass(new RegisterListenersPass());
  10. $containerBuilder->register('event_dispatcher', EventDispatcher::class);
  11. // registers an event listener
  12. $containerBuilder->register('listener_service_id', \AcmeListener::class)
  13. ->addTag('kernel.event_listener', [
  14. 'event' => 'acme.foo.action',
  15. 'method' => 'onFooAction',
  16. ]);
  17. // registers an event subscriber
  18. $containerBuilder->register('subscriber_service_id', \AcmeSubscriber::class)
  19. ->addTag('kernel.event_subscriber');

RegisterListenersPass resolves aliased class names which for instance allows to refer to an event via the fully qualified class name (FQCN) of the event class. The pass will read the alias mapping from a dedicated container parameter. This parameter can be extended by registering another compiler pass, AddEventAliasesPass:

  1. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  2. use Symfony\Component\DependencyInjection\ContainerBuilder;
  3. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  4. use Symfony\Component\DependencyInjection\Reference;
  5. use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
  6. use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
  7. use Symfony\Component\EventDispatcher\EventDispatcher;
  8. $containerBuilder = new ContainerBuilder(new ParameterBag());
  9. $containerBuilder->addCompilerPass(new AddEventAliasesPass([
  10. \AcmeFooActionEvent::class => 'acme.foo.action',
  11. ]));
  12. $containerBuilder->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
  13. $containerBuilder->register('event_dispatcher', EventDispatcher::class);
  14. // registers an event listener
  15. $containerBuilder->register('listener_service_id', \AcmeListener::class)
  16. ->addTag('kernel.event_listener', [
  17. // will be translated to 'acme.foo.action' by RegisterListenersPass.
  18. 'event' => \AcmeFooActionEvent::class,
  19. 'method' => 'onFooAction',
  20. ]);

Note

Note that AddEventAliasesPass has to be processed before RegisterListenersPass.

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, that event subscribers are tagged with the kernel.event_subscriber tag and that the alias mapping is stored as parameter event_dispatcher.event_aliases. You can change these default values by passing custom values to the constructors of RegisterListenersPass and AddEventAliasesPass.

Creating and Dispatching an Event

In addition to registering listeners with existing events, you can create and dispatch your own events. This is useful when creating third-party libraries and also when you want to keep different components of your own system flexible and decoupled.

Creating an Event Class

Suppose you want to create a new event - order.placed - that is dispatched each time a customer orders a product with your application. When dispatching this event, you’ll pass a custom event instance that has access to the placed order. Start by creating this custom event class and documenting it:

  1. namespace Acme\Store\Event;
  2. use Acme\Store\Order;
  3. use Symfony\Contracts\EventDispatcher\Event;
  4. /**
  5. * The order.placed event is dispatched each time an order is created
  6. * in the system.
  7. */
  8. class OrderPlacedEvent extends Event
  9. {
  10. public const NAME = 'order.placed';
  11. protected $order;
  12. public function __construct(Order $order)
  13. {
  14. $this->order = $order;
  15. }
  16. public function getOrder(): Order
  17. {
  18. return $this->order;
  19. }
  20. }

Each listener now has access to the order via the `getOrder() method.

Note

If you don’t need to pass any additional data to the event listeners, you can also use the default Symfony\Contracts\EventDispatcher\Event class. In such case, you can document the event and its name in a generic StoreEvents class, similar to the Symfony\Component\HttpKernel\KernelEvents class.

Dispatch the Event

The dispatch() method notifies all listeners of the given event. It takes two arguments: the Event instance to pass to each listener of that event and the name of the event to dispatch:

  1. use Acme\Store\Event\OrderPlacedEvent;
  2. use Acme\Store\Order;
  3. // the order is somehow created or retrieved
  4. $order = new Order();
  5. // ...
  6. // creates the OrderPlacedEvent and dispatches it
  7. $event = new OrderPlacedEvent($order);
  8. $dispatcher->dispatch($event, OrderPlacedEvent::NAME);

Notice that the special OrderPlacedEvent object is created and passed to the dispatch() method. Now, any listener to theorder.placedevent will receive theOrderPlacedEvent`.

Using Event Subscribers

The most common way to listen to an event is to register an event listener with the dispatcher. This listener can listen to one or more events and is notified each time those events are dispatched.

Another way to listen to events is via an event subscriber. An event subscriber is a PHP class that’s able to tell the dispatcher exactly which events it should subscribe to. It implements the Symfony\Component\EventDispatcher\EventSubscriberInterface interface, which requires a single static method called getSubscribedEvents(). Take the following example of a subscriber that subscribes to the kernel.response and order.placed events:

  1. namespace Acme\Store\Event;
  2. use Acme\Store\Event\OrderPlacedEvent;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class StoreSubscriber implements EventSubscriberInterface
  7. {
  8. public static function getSubscribedEvents()
  9. {
  10. return [
  11. KernelEvents::RESPONSE => [
  12. ['onKernelResponsePre', 10],
  13. ['onKernelResponsePost', -10],
  14. ],
  15. OrderPlacedEvent::NAME => 'onStoreOrder',
  16. ];
  17. }
  18. public function onKernelResponsePre(ResponseEvent $event)
  19. {
  20. // ...
  21. }
  22. public function onKernelResponsePost(ResponseEvent $event)
  23. {
  24. // ...
  25. }
  26. public function onStoreOrder(OrderPlacedEvent $event)
  27. {
  28. // ...
  29. }
  30. }

This is very similar to a listener class, except that the class itself can tell the dispatcher which events it should listen to. To register a subscriber with the dispatcher, use the addSubscriber() method:

  1. use Acme\Store\Event\StoreSubscriber;
  2. // ...
  3. $subscriber = new StoreSubscriber();
  4. $dispatcher->addSubscriber($subscriber);

The dispatcher will automatically register the subscriber for each event returned by the getSubscribedEvents() method. This method returns an array indexed by event names and whose values are either the method name to call or an array composed of the method name to call and a priority (a positive or negative integer that defaults to0`).

The example above shows how to register several listener methods for the same event in subscriber and also shows how to pass the priority of each listener method. The higher the number, the earlier the method is called. In the above example, when the kernel.response event is triggered, the methods onKernelResponsePre() andonKernelResponsePost() are called in that order.

Stopping Event Flow/Propagation

In some cases, it may make sense for a listener to prevent any other listeners from being called. In other words, the listener needs to be able to tell the dispatcher to stop all propagation of the event to future listeners (i.e. to not notify any more listeners). This can be accomplished from inside a listener via the stopPropagation() method:

  1. use Acme\Store\Event\OrderPlacedEvent;
  2. public function onStoreOrder(OrderPlacedEvent $event)
  3. {
  4. // ...
  5. $event->stopPropagation();
  6. }

Now, any listeners to order.placed that have not yet been called will not be called.

It is possible to detect if an event was stopped by using the isPropagationStopped() method which returns a boolean value:

  1. // ...
  2. $dispatcher->dispatch($event, 'foo.event');
  3. if ($event->isPropagationStopped()) {
  4. // ...
  5. }

EventDispatcher Aware Events and Listeners

The EventDispatcher always passes the dispatched event, the event’s name and a reference to itself to the listeners. This can lead to some advanced applications of the EventDispatcher including dispatching other events inside listeners, chaining events or even lazy loading listeners into the dispatcher object.

Event Name Introspection

The EventDispatcher instance, as well as the name of the event that is dispatched, are passed as arguments to the listener:

  1. use Symfony\Contracts\EventDispatcher\Event;
  2. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  3. class Foo
  4. {
  5. public function myEventListener(Event $event, $eventName, EventDispatcherInterface $dispatcher)
  6. {
  7. // ... do something with the event name
  8. }
  9. }

Other Dispatchers

Besides the commonly used EventDispatcher, the component comes with some other dispatchers:

Learn More

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.