Symfony 6 Certification New exam with updated questions 100% online Show your expertise

New in Symfony 2.6: Error page previews

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

Contributed by
webfactory GmbH
in #12096.

User Experience (UX) has become an essential part of web application development. One of the most important elements of UX is to provide comprehensible and helpful error pages in case things don't work as expected.

Developing and designing these error pages in previous Symfony versions was a bit involved because it required to trigger an exception with the exact HTTP code needed and you had to make the exception controller use the right template.

That's why Symfony 2.6 will include a new option to preview error pages. Specifically, TwigBundle now contains a routing file that you can import from your routing_dev.yml file:

# app/config/routing_dev.yml
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error

Once these new routes are loaded, you can preview any error page using these special URLs:

http://localhost/app_dev.php/_error/{statusCode} http://localhost/app_dev.php/_error/{statusCode}.{format}

The statusCode corresponds to the HTTP Status Code of the error and the format option defaults to html and accepts any valid request format, such as json.

To learn more about customizing your error pages, read the revamped cookbook article about How to Customize Error Pages. Now it's time to test this feature and don't forget to leave a link to your beautiful error page in the comments of this article!

Note: this post was mostly written by Matthias Pigulla.

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.


In case you'd like to give this a try: There is still an open PR with a small fix for the case that you're using a custom exception controller:
Very helpful when designing beautiful excpetion pages. Thanks.

"Sorry, it's broken.. but so pretty!" ;)
Great idea
This is great! We had a very hacky solution, glad to get rid of it!
Useful indead, instead of throwing exceptions by hand, switch the debug flag... It was boring. Ty.
I just tried this with my brand new project, I get this:

Catchable Fatal Error: Argument 2 passed to Symfony\Bundle\TwigBundle\Controller\ExceptionController::showAction() must be an instance of Symfony\Component\HttpKernel\Exception\FlattenException, instance of Symfony\Component\Debug\Exception\FlattenException given
@Massimiliano Arione, same here.

Seems to be a namespace problem.

You can change it in @TwigBundle/Controller/PreviewErrorController.php line 14, replace "Debug" by "HttpKernel".

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.