Symfony Events reference
In a nutshell
The Symfony framework is essentially an HTTP request-response framework. While handling an incoming HTTP request, the framework dispatches some events that you can use to modify the request or response.
For example, Symfony dispatches several events during the lifecycle of HTTP requests:
- When a request has been created.
- When a controller action is about to be executed.
- When a response is ready to be sent.
- When an exception has been thrown.
You can create listeners that listen to such events and execute some logic. For example, you might create a listener that listens to exceptions and sends you an email when an exception is thrown.
Many Symfony components like Security, Messenger, Workflow, and Mailer use events extensively – either by dispatching their own events or listening to events from other Symfony components. Third-party packages or bundles can also dispatch their own events to make their code extensible.
Kernel events
Each event dispatched by the HttpKernel component is defined as a constant on the KernelEvents
class. Each event listener is passed a single argument, which is some subclass of KernelEvent
, which gives you the following information:
getRequestType()
– Returns the type of the request (e.g.,HttpKernelInterface::MAIN_REQUEST
orHttpKernelInterface::SUB_REQUEST
)getKernel()
– Returns the Kernel handling the request.getRequest()
– Returns the currentRequest
being handled.isMainRequest()
– Checks if this is a main request.
How to create custom subscribers
Let’s create a subscriber that listens to the Symfony\Component\HttpKernel\Event\ControllerEvent
event, which is dispatched just before the controller is called.
1. Generate a subscriber
symfony console make:subscriber ControllerEventSubscriber
The command will ask you what event you want to listen to. Enter the event name (e.g., Symfony\Component\HttpKernel\Event\ControllerEvent
).
2. Implement subscriber logic
The make
command will generate a subscriber file: src/EventSubscriber/ControllerEventSubscriber.php
:
<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
class ControllerEventSubscriber implements EventSubscriberInterface
{
public function onControllerEvent(ControllerEvent $event): void
{
// do something...
}
public static function getSubscribedEvents(): array
{
return [
ControllerEvent::class => 'onControllerEvent',
];
}
}
Add your custom logic to the onControllerEvent
method. You will have access to the event context (ControllerEvent
).
List all registered event listeners
bin/console debug:event-dispatcher
List registered listeners for a particular event
bin/console debug:event-dispatcher <event-name>
For example, list all listeners for the kernel.controller
event (see list of built-in Symfony events).
bin/console debug:event-dispatcher kernel.controller
Events reference
Name | KernelEvents Constant | Argument passed to the listener |
---|---|---|
kernel.request | KernelEvents::REQUEST | RequestEvent |
kernel.controller | KernelEvents::CONTROLLER | ControllerEvent |
kernel.controller_arguments | KernelEvents::CONTROLLER_ARGUMENTS | ControllerArgumentsEvent |
kernel.view | KernelEvents::VIEW | ViewEvent |
kernel.response | KernelEvents::RESPONSE | ResponseEvent |
kernel.finish_request | KernelEvents::FINISH_REQUEST | FinishRequestEvent |
kernel.terminate | KernelEvents::TERMINATE | TerminateEvent |
kernel.exception | KernelEvents::EXCEPTION | ExceptionEvent |
Sources
- Diagram
- EventDispatcher component – Symfony docs
- Built-in Symfony Events – Symfony docs
- Events and Event Listeners – Symfony docs
- Lifecycle diagram
- Built-in Symfony Events – Symfony docs
- Symfony events: kernel.request reference
- Symfony events: kernel.controller reference
- Symfony events: kernel.controller_arguments reference
- Symfony events: kernel.view reference
- Symfony events: kernel.response reference
- Symfony events: kernel.finish_request reference
- Symfony events: kernel.terminate reference
- Symfony events: kernel.exception reference
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment