Event dispatcher
Event dispatcher
// src/EventListener/ExceptionListener.php namespace App\EventListener; class ExceptionListener { public function __invoke(ExceptionEvent $event): void // this event listener listens to Excepction events { // You get the exception object from the received event $exception = $event->getThrowable(); // ... // sends the modified response object to the event $event->setResponse($response); } }# config/services.yaml services: App\EventListener\ExceptionListener: # register the class and tell symfony that is an event listener tags: [kernel.event_listener] # or { name: kernel.event_listener, event: kernel.exception (if not typ), method: methodName }namespace App\EventListener; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; #[AsEventListener(event: CustomEvent::class, method: 'onCustomEvent')] #[AsEventListener(event: 'foo', priority: 42)] #[AsEventListener(event: 'bar', method: 'onBarEvent')] final class MyMultiListener { public function onCustomEvent(CustomEvent $event): void { // ... } public function onFoo(): void { // ... } public function onBarEvent(): void { // ... } #[AsEventListener(event: 'bar')] public function onBarEvent(): void { // ... } }