Creative Commons License
This work is licensed under a
Creative Commons
Attribution-Share Alike 3.0
Unported License.

Master Symfony2 fundamentals

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

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

How to customize Error Pages

When any exception is thrown in Symfony2, the exception is caught inside the Kernel class and eventually forwarded to a special controller, TwigBundle:Exception:show for handling. This controller, which lives inside the core TwigBundle, determines which error template to display and the status code that should be set for the given exception.

Error pages can be customized in two different ways, depending on how much control you need:

  1. Customize the error templates of the different error pages (explained below);
  2. Replace the default exception controller twig.controller.exception:showAction with your own controller and handle it however you want (see exception_controller in the Twig reference). The default exception controller is registered as a service - the actual class is Symfony\Bundle\TwigBundle\Controller\ExceptionController.

Tip

The customization of exception handling is actually much more powerful than what's written here. An internal event, kernel.exception, is thrown which allows complete control over exception handling. For more information, see kernel.exception Event.

The default ExceptionController will either display an exception or error page, depending on the setting of the kernel.debug flag. While exception pages give you a lot of helpful information during development, error pages are meant to be shown to the end-user.

You should not set kernel.debug to false in order to see your error pages during development. This will also stop Symfony2 from recompiling your twig templates, among other things.

The third-party WebfactoryExceptionsBundle provides a special test controller that allows you to display your custom error pages for arbitrary HTTP status codes even with kernel.debug set to true.

All of the error templates live inside the TwigBundle. To override the templates, simply rely on the standard method for overriding templates that live inside a bundle. For more information, see Overriding Bundle Templates.

For example, to override the default error template, create a new template located at app/Resources/TwigBundle/views/Exception/error.html.twig:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>An Error Occurred: {{ status_text }}</title>
</head>
<body>
    <h1>Oops! An Error Occurred</h1>
    <h2>The server returned a "{{ status_code }} {{ status_text }}".</h2>
</body>
</html>

Caution

You must not use is_granted in your error pages (or layout used by your error pages), because the router runs before the firewall. If the router throws an exception (for instance, when the route does not match), then using is_granted will throw a further exception. You can use is_granted safely by saying {% if app.user and is_granted('...') %}.

Tip

If you're not familiar with Twig, don't worry. Twig is a simple, powerful and optional templating engine that integrates with Symfony2. For more information about Twig see Creating and Using Templates.

In addition to the standard HTML error page, Symfony provides a default error page for many of the most common response formats, including JSON (error.json.twig), XML (error.xml.twig) and even JavaScript (error.js.twig), to name a few. To override any of these templates, just create a new file with the same name in the app/Resources/TwigBundle/views/Exception directory. This is the standard way of overriding any template that lives inside a bundle.

Customizing the 404 Page and other Error Pages

You can also customize specific error templates according to the HTTP status code. For instance, create a app/Resources/TwigBundle/views/Exception/error404.html.twig template to display a special page for 404 (page not found) errors.

Symfony uses the following algorithm to determine which template to use:

  • First, it looks for a template for the given format and status code (like error404.json.twig);
  • If it does not exist, it looks for a template for the given format (like error.json.twig);
  • If it does not exist, it falls back to the HTML template (like error.html.twig).

Tip

To see the full list of default error templates, see the Resources/views/Exception directory of the TwigBundle. In a standard Symfony2 installation, the TwigBundle can be found at vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle. Often, the easiest way to customize an error page is to copy it from the TwigBundle into app/Resources/TwigBundle/views/Exception and then modify it.

Note

The debug-friendly exception pages shown to the developer can even be customized in the same way by creating templates such as exception.html.twig for the standard HTML exception page or exception.json.twig for the JSON exception page.