Symfony 7.4 EventDispatcher component reference for event-driven communication between application components.
GitHub: https://github.com/symfony/event-dispatcher Docs: https://symfony.com/doc/7.4/components/event_dispatcher.html
use Symfony\Contracts\EventDispatcher\Event;
final class OrderPlacedEvent extends Event
{
public function __construct(private Order $order) {}
public function getOrder(): Order
{
return $this->order;
}
}
$event = new OrderPlacedEvent($order);
$dispatcher->dispatch($event);
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener]
final class OrderListener
{
public function __invoke(OrderPlacedEvent $event): void
{
// handle event
}
}
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
final class MyMultiListener
{
#[AsEventListener]
public function onOrderPlaced(OrderPlacedEvent $event): void { /* ... */ }
#[AsEventListener(event: 'foo', priority: 42)]
public function onFoo(): void { /* ... */ }
}
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class StoreSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => [
['onKernelResponsePre', 10],
['onKernelResponsePost', -10],
],
OrderPlacedEvent::class => 'onPlacedOrder',
];
}
// ... handler methods
}
public function onPlacedOrder(OrderPlacedEvent $event): void
{
$event->stopPropagation();
}
kernel.request (KernelEvents::REQUEST)kernel.controller (KernelEvents::CONTROLLER)kernel.response (KernelEvents::RESPONSE)kernel.exception (KernelEvents::EXCEPTION)php bin/console debug:event-dispatcher
php bin/console debug:event-dispatcher kernel.exception
For complete details including service container integration, event aliases, before/after filters, listener vs subscriber comparison, all kernel event types, priority system, and advanced patterns, see references/event-dispatcher.md.