symfony3Event Dispatcher


Syntax

  • $dispatcher->dispatch(string $eventName, Event $event);
  • $dispatcher->addListener(string $eventName, callable $listener, int $priority = 0);
  • $dispatcher->addSubscriber(EventSubscriberInterface $subscriber);

Remarks

  • It is often best to use a single instance of EventDispatcher in your application that you inject into the objects that need to fire events.
  • It is best practice to have a single location where you manage the configuration of, and add event listeners to, your EventDispatcher. The Symfony framework uses the Dependency Injection Container.
  • These patterns will allow you to easily change your event listeners without needing to change the code of any module that is dispatching events.
  • The decoupling of event dispatch from event listener configuration is what makes the Symfony EventDispatcher so powerful
  • The EventDispatcher helps you satisfy the Open/Closed Principle.