Built-in Symfony Events
The Symfony framework is an HTTP Request-Response one. During the handling of an HTTP request, the framework (or any application using the HttpKernel component) dispatches some events which you can use to modify how the request is handled and how the response is returned.
Kernel Events
Each event dispatched by the HttpKernel component is a subclass of KernelEvent, which provides the following information:
- getRequestType()
-
Returns the type of the request (
HttpKernelInterface::MAIN_REQUESTorHttpKernelInterface::SUB_REQUEST). - getKernel()
- Returns the Kernel handling the request.
- getRequest()
-
Returns the current
Requestbeing handled. - isMainRequest()
- Checks if this is a main request.
kernel.request
Event Class: RequestEvent
This event is dispatched very early in Symfony, before the controller is determined. It's useful to add information to the Request or return a Response early to stop the handling of the request.
See also
Read more on the kernel.request event.
Execute this command to find out which listeners are registered for this event and their priorities:
1
$ php bin/console debug:event-dispatcher kernel.request
kernel.controller
Event Class: ControllerEvent
This event is dispatched after the controller has been resolved but before executing it. It's useful to initialize things later needed by the controller, such as value resolvers, and even to change the controller entirely:
1 2 3 4 5 6 7 8 9
use Symfony\Component\HttpKernel\Event\ControllerEvent;
public function onKernelController(ControllerEvent $event): void
{
// ...
// the controller can be changed to any PHP callable
$event->setController($myCustomController);
}
You can also use getAttributes() to retrieve PHP attributes declared on the
controller:
1 2 3 4 5 6 7 8 9 10 11 12 13
use Symfony\Component\HttpKernel\Event\ControllerEvent;
public function onKernelController(ControllerEvent $event): void
{
// get all attributes, grouped by class name
$allAttributes = $event->getAttributes();
// get all attributes as a flat list (not grouped)
$flatAttributes = $event->getAttributes('*');
// get attributes of a specific class
$cacheAttributes = $event->getAttributes(Cache::class);
}
Controller attributes are stored in the _controller_attributes request
attribute, which means they can be overridden at runtime to change the behavior
of attribute-based listeners without modifying the controller source code.
8.1
Storing controller attributes in the _controller_attributes request
attribute was introduced in Symfony 8.1.
The ControllerEvent class provides
the evaluate() method,
which standardizes how Expression and Closure values found in controller
attributes are evaluated. If the given value is a Closure, it is called with
($args, $request, $controller) as arguments. If it is an
Expression, it is evaluated with
the variables request, args and this (the controller object). Any
other value is returned unchanged:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use Symfony\Component\HttpKernel\Event\ControllerEvent;
public function onKernelController(ControllerEvent $event): void
{
// evaluate a Closure defined in a controller attribute
$result = $event->evaluate(
static function (array $args, Request $request, object $controller): bool {
return $request->attributes->get('_route') === 'admin';
}
);
// evaluate an Expression defined in a controller attribute
// available variables: "request", "args", and "this" (the controller)
$result = $event->evaluate(new Expression('request.getMethod() == "POST"'));
}
8.1
The evaluate() method was introduced in Symfony 8.1.
See also
Read more on the kernel.controller event.
Execute this command to find out which listeners are registered for this event and their priorities:
1
$ php bin/console debug:event-dispatcher kernel.controller
kernel.controller_arguments
Event Class: ControllerArgumentsEvent
This event is dispatched just before a controller is called. It's useful to
configure the arguments that are going to be passed to the controller.
Typically, this is used to map URL routing parameters to their corresponding
named arguments; or pass the current request when the Request type-hint is
found:
1 2 3 4 5 6 7 8 9 10 11 12 13
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
{
// ...
// get controller and request arguments
$namedArguments = $event->getRequest()->attributes->all();
$controllerArguments = $event->getArguments();
// set the controller arguments to modify the original arguments or add new ones
$event->setArguments($newArguments);
}
The ControllerArgumentsEvent also
provides the evaluate()
method (inherited from ControllerEvent) to evaluate Expression and
Closure values found in controller attributes.
8.1
The evaluate() method was introduced in Symfony 8.1.
Execute this command to find out which listeners are registered for this event and their priorities:
1
$ php bin/console debug:event-dispatcher kernel.controller_arguments
Accessing Controller Metadata in Events
All kernel events dispatched after controller resolution (kernel.controller_arguments,
kernel.view, kernel.response, kernel.finish_request and
kernel.exception) provide access to the controller metadata through the
controllerMetadata public property. This allows listeners to inspect the
controller's attributes and arguments without using reflection on the controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
use Symfony\Component\HttpKernel\Event\ResponseEvent;
public function onKernelResponse(ResponseEvent $event): void
{
$metadata = $event->controllerMetadata;
if (null === $metadata) {
return;
}
// get the PHP attributes declared on the controller
$attributes = $metadata->getAttributes();
// if the event happens after controller_arguments, you also get
// access to the resolved controller arguments
$namedArguments = $metadata->getNamedArguments();
}
8.1
The controllerMetadata property was introduced in Symfony 8.1.
kernel.view
Event Class: ViewEvent
This event is dispatched after the controller has been executed but only if
the controller does not return a Response
object. It's useful to transform the returned value (e.g. a string with some
HTML contents) into the Response object needed by Symfony:
1 2 3 4 5 6 7 8 9 10 11 12
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ViewEvent;
public function onKernelView(ViewEvent $event): void
{
$value = $event->getControllerResult();
$response = new Response();
// ... somehow customize the Response from the return value
$event->setResponse($response);
}
See also
Read more on the kernel.view event.
Execute this command to find out which listeners are registered for this event and their priorities:
1
$ php bin/console debug:event-dispatcher kernel.view
kernel.response
Event Class: ResponseEvent
This event is dispatched after the controller or any kernel.view listener
returns a Response object. It's useful to modify or replace the response
before sending it back (e.g. add/modify HTTP headers, add cookies, etc.):
1 2 3 4 5 6 7 8
use Symfony\Component\HttpKernel\Event\ResponseEvent;
public function onKernelResponse(ResponseEvent $event): void
{
$response = $event->getResponse();
// ... modify the response object
}
You can use getControllerAttributes() to retrieve PHP attributes declared
on the controller that handled the request:
1 2 3 4 5 6 7 8 9 10 11 12 13
use Symfony\Component\HttpKernel\Event\ResponseEvent;
public function onKernelResponse(ResponseEvent $event): void
{
// get all controller attributes, grouped by class name
$allAttributes = $event->getControllerAttributes();
// get all controller attributes as a flat list (not grouped)
$flatAttributes = $event->getControllerAttributes('*');
// get attributes of a specific class
$cacheAttributes = $event->getControllerAttributes(Cache::class);
}
8.1
The ResponseEvent::getControllerAttributes() method was introduced in
Symfony 8.1.
See also
Read more on the kernel.response event.
Execute this command to find out which listeners are registered for this event and their priorities:
1
$ php bin/console debug:event-dispatcher kernel.response
kernel.finish_request
Event Class: FinishRequestEvent
This event is dispatched after the kernel.response event. It's useful to reset
the global state of the application (for example, the translator listener resets
the translator's locale to the one of the parent request):
1 2 3 4 5 6 7 8 9 10 11
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
public function onKernelFinishRequest(FinishRequestEvent $event): void
{
if (null === $parentRequest = $this->requestStack->getParentRequest()) {
return;
}
// reset the locale of the subrequest to the locale of the parent request
$this->setLocale($parentRequest);
}
Execute this command to find out which listeners are registered for this event and their priorities:
1
$ php bin/console debug:event-dispatcher kernel.finish_request
kernel.terminate
Event Class: TerminateEvent
This event is dispatched after the response has been sent (after the execution of the handle() method). It's useful to perform slow or complex tasks that don't need to be completed to send the response (e.g. sending emails).
See also
Read more on the kernel.terminate event.
Execute this command to find out which listeners are registered for this event and their priorities:
1
$ php bin/console debug:event-dispatcher kernel.terminate
kernel.exception
Event Class: ExceptionEvent
This event is dispatched as soon as an error occurs during the handling of the HTTP request. It's useful to recover from errors or modify the exception details sent as response:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
$response = new Response();
// setup the Response object based on the caught exception
$event->setResponse($response);
// you can alternatively set a new Exception
// $exception = new \Exception('Some special exception');
// $event->setThrowable($exception);
}
Note
The TwigBundle registers an ErrorListener
that forwards the Request to a given controller defined by the
exception_listener.controller parameter.
Symfony uses the following logic to determine the HTTP status code of the response:
- If isClientError(),
isServerError() or
isRedirect() is true,
then the status code on your
Responseobject is used; - If the original exception implements
HttpExceptionInterface,
then
getStatusCode()is called on the exception and used (the headers fromgetHeaders()are also added); - If both of the above aren't true, then a 500 status code is used.
Note
If you want to overwrite the status code of the exception response, which
you should not without a good reason, call
ExceptionEvent::allowCustomResponseCode() first and then
set the status code on the response:
1 2 3
$event->allowCustomResponseCode();
$response = new Response('No Content', 204);
$event->setResponse($response);
The status code sent to the client in the above example will be 204. If
$event->allowCustomResponseCode() is omitted, then the kernel will set
an appropriate status code based on the type of exception thrown.
See also
Read more on the kernel.exception event.
Execute this command to find out which listeners are registered for this event and their priorities:
1
$ php bin/console debug:event-dispatcher kernel.exception