Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Reference
  4. Built-in Symfony Events
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Kernel Events
    • kernel.request
    • kernel.controller
    • kernel.view
    • kernel.response
    • kernel.finish_request
    • kernel.terminate
    • kernel.exception

Built-in Symfony Events

Edit this page

Warning: You are browsing the documentation for Symfony 2.8, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

Built-in Symfony Events

During the handling of an HTTP request, the Symfony framework (or any application using the HttpKernel component) dispatches some events which you can use to modify how the request is handled.

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::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST).
getKernel()
Returns the Kernel handling the request.
getRequest()
Returns the current Request being handled.

kernel.request

Event Class: GetResponseEvent

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 app/console debug:event-dispatcher kernel.request

kernel.controller

Event Class: FilterControllerEvent

This event is dispatched after the controller to be executed has been resolved but before executing it. It's useful to initialize things later needed by the controller, such as param converters, and even to change the controller entirely:

1
2
3
4
5
6
7
8
9
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

public function onKernelController(FilterControllerEvent $event)
{
    // ...

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

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 app/console debug:event-dispatcher kernel.controller

kernel.view

Event Class: GetResponseForControllerResultEvent

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\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpFoundation\Response;

public function onKernelView(GetResponseForControllerResultEvent $event)
{
    $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 app/console debug:event-dispatcher kernel.view

kernel.response

Event Class: FilterResponseEvent

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
public function onKernelResponse(FilterResponseEvent $event)
{
    $response = $event->getResponse();

    // ... modify the response object
}

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 app/console debug:event-dispatcher kernel.response

kernel.finish_request

Event Class: FinishRequestEvent

This event is dispatched after a sub request has finished. 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
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);
}

Execute this command to find out which listeners are registered for this event and their priorities:

1
$ php app/console debug:event-dispatcher kernel.finish_request

kernel.terminate

Event Class: PostResponseEvent

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 app/console debug:event-dispatcher kernel.terminate

kernel.exception

Event Class: GetResponseForExceptionEvent

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\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

The TwigBundle registers an ExceptionListener that forwards the Request to a given controller defined by the exception_listener.controller parameter.

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 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.

Execute this command to find out which listeners are registered for this event and their priorities:

1
$ php app/console debug:event-dispatcher kernel.exception
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Code consumes server resources. Blackfire tells you how

Code consumes server resources. Blackfire tells you how

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Rico Neitzel, a Symfony contributor

Thanks Rico Neitzel for being a Symfony contributor

2 commits • 6 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia