WARNING: You are browsing the documentation for Symfony 3.2 which is not maintained anymore. Consider upgrading your projects to Symfony 5.1.

Symfony Framework Events

Symfony Framework Events

When the Symfony Framework (or anything using the Symfony\Component\HttpKernel\HttpKernel) handles a request, a few core events are dispatched so that you can add listeners throughout the process. These are called the “kernel events”. For a larger explanation, see The HttpKernel Component.

Kernel Events

Each event dispatched by the kernel is a subclass of Symfony\Component\HttpKernel\Event\KernelEvent. This means that each event has access to the following information:

getRequestType()
Returns the type of the request (HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST).
getKernel()
Returns the Kernel handling the request.
getRequest()
Returns the current Request being handled.

kernel.request

Event Class: Symfony\Component\HttpKernel\Event\GetResponseEvent

This event is dispatched very early in Symfony, before the controller is determined.

See also

Read more on the kernel.request event.

These are the built-in Symfony listeners registered to this event:

Listener Class Name Priority
Symfony\Component\HttpKernel\EventListener\ProfilerListener 1024
Symfony\Bundle\FrameworkBundle\EventListener\TestSessionListener 192
Symfony\Bundle\FrameworkBundle\EventListener\SessionListener 128
Symfony\Component\HttpKernel\EventListener\RouterListener 32
Symfony\Component\HttpKernel\EventListener\LocaleListener 16
Symfony\Component\Security\Http\Firewall 8

kernel.controller

Event Class: Symfony\Component\HttpKernel\Event\FilterControllerEvent

This event can be an entry point used to modify the controller that should be executed:

use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

public function onKernelController(FilterControllerEvent $event)
{
    $controller = $event->getController();
    // ...

    // the controller can be changed to any PHP callable
    $event->setController($controller);
}

See also

Read more on the kernel.controller event.

This is the built-in Symfony listener related to this event:

Listener Class Name Priority
Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector 0

kernel.view

Event Class: Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent

This event is not used by the FrameworkBundle, but it can be used to implement a view sub-system. This event is called only if the Controller does not return a Response object. The purpose of the event is to allow some other return value to be converted into a Response.

The value returned by the Controller is accessible via the getControllerResult() method:

use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpFoundation\Response;

public function onKernelView(GetResponseForControllerResultEvent $event)
{
    $val = $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.

kernel.response

Event Class: Symfony\Component\HttpKernel\Event\FilterResponseEvent

The purpose of this event is to allow other systems to modify or replace the Response object after its creation:

public function onKernelResponse(FilterResponseEvent $event)
{
    $response = $event->getResponse();

    // ... modify the response object
}

The FrameworkBundle registers several listeners:

Symfony\Component\HttpKernel\EventListener\ProfilerListener
Collects data for the current request.
Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener
Injects the Web Debug Toolbar.
Symfony\Component\HttpKernel\EventListener\ResponseListener
Fixes the Response Content-Type based on the request format.
Symfony\Component\HttpKernel\EventListener\EsiListener
Adds a Surrogate-Control HTTP header when the Response needs to be parsed for ESI tags.

See also

Read more on the kernel.response event.

These are the built-in Symfony listeners registered to this event:

Listener Class Name Priority
Symfony\Component\HttpKernel\EventListener\EsiListener 0
Symfony\Component\HttpKernel\EventListener\ResponseListener 0
Symfony\Component\Security\Http\RememberMe\ResponseListener 0
Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector 0
Symfony\Component\HttpKernel\EventListener\ProfilerListener -100
Symfony\Bundle\FrameworkBundle\EventListener\TestSessionListener -128
Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener -128
Symfony\Component\HttpKernel\EventListener\StreamedResponseListener -1024

kernel.finish_request

Event Class: Symfony\Component\HttpKernel\Event\FinishRequestEvent

The purpose of this event is to allow you to reset the global and environmental state of the application after a sub-request has finished (for example, the translator listener resets the translator’s locale to the one of the parent request):

public function onKernelFinishRequest(FinishRequestEvent $event)
{
    if (null === $parentRequest = $this->requestStack->getParentRequest()) {
        return;
    }

    //Reset the locale of the subrequest to the locale of the parent request
    $this->setLocale($parentRequest);
}

These are the built-in Symfony listeners related to this event:

Listener Class Name Priority
Symfony\Component\HttpKernel\EventListener\LocaleListener 0
Symfony\Component\HttpKernel\EventListener\TranslatorListener 0
Symfony\Component\HttpKernel\EventListener\RouterListener 0
Symfony\Component\Security\Http\Firewall 0

kernel.terminate

Event Class: Symfony\Component\HttpKernel\Event\PostResponseEvent

The purpose of this event is to perform tasks after the response was already served to the client.

See also

Read more on the kernel.terminate event.

This is the built-in Symfony listener related to this event:

Listener Class Name Priority
EmailSenderListener 0

kernel.exception

Event Class: Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent

The TwigBundle registers an Symfony\Component\HttpKernel\EventListener\ExceptionListener that forwards the Request to a given controller defined by the exception_listener.controller parameter.

A listener on this event can create and set a Response object, create and set a new Exception object, or do nothing:

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;

public function onKernelException(GetResponseForExceptionEvent $event)
{
    $exception = $event->getException();
    $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->setException($exception);
}

Note

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 Response object is used;
  • If the original exception implements Symfony\Component\HttpKernel\Exception\HttpExceptionInterface, then getStatusCode() is called on the exception and used (the headers from getHeaders() are also added);
  • If both of the above aren’t true, then a 500 status code is used.

See also

Read more on the kernel.exception event.

These are the built-in Symfony listeners registered to this event:

Listener Class Name Priority
Symfony\Component\HttpKernel\EventListener\ProfilerListener 0
Symfony\Component\HttpKernel\EventListener\ExceptionListener -128

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.