@@ -27,7 +27,7 @@ Error pages for the production environment can be customized in different ways
2727depending on your needs:
2828
2929#. If you just want to change the contents and styles of the error pages to match
30- the rest of your application, :ref: `override default error templates <use-default-exception-controller >`;
30+ the rest of your application, :ref: `override the default error templates <use-default-exception-controller >`;
3131
3232#. If you also want to tweak the logic used by Symfony to generate error pages,
3333 :ref: `override the default exception controller <custom-exception-controller >`;
@@ -41,25 +41,16 @@ depending on your needs:
4141Overriding the Default Error Templates
4242--------------------------------------
4343
44- By default, the ``showAction() `` method of the
45- :class: `Symfony\\ Bundle\\ TwigBundle\\ Controller\\ ExceptionController ` is called
46- whenever an exception occurs, thanks to an event listener configured by the TwigBundle.
47-
48- Then, the controller selects one of the templates defined in the
49- ``Resources/views/Exception `` directory of the TwigBundle to render the error
50- page. If you browse that directory (usually located in
51- ``vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle ``) you'll find a lot of
52- templates defined for different types of errors and content formats
53- (``error.*.twig `` templates are used in the production environment whereas
54- ``exception.*.twig `` templates are used in the development environment).
44+ When the error page loads, an internal :class: `Symfony\\ Bundle\\ TwigBundle\\ Controller\\ ExceptionController `
45+ is used to render a Twig template to show the user.
5546
5647.. _cookbook-error-pages-by-status-code :
5748
58- The logic followed by the `` ExceptionController `` to pick one of the available
59- templates is based on the HTTP status code and the request format :
49+ This controller uses the HTTP status code, request format and the following
50+ logic to determine the template filename :
6051
6152#. Look for a template for the given format and status code (like ``error404.json.twig ``
62- or ``error500.xml .twig ``);
53+ or ``error500.html .twig ``);
6354
6455#. If the previous template doesn't exist, discard the status code and look for
6556 a generic template for the given format (like ``error.json.twig `` or
@@ -71,8 +62,29 @@ templates is based on the HTTP status code and the request format:
7162.. _overriding-or-adding-templates :
7263
7364To override these templates, simply rely on the standard Symfony method for
74- :ref: `overriding templates that live inside a bundle <overriding-bundle-templates >`.
75- For example, to override the 404 error template for HTML pages, create a new
65+ :ref: `overriding templates that live inside a bundle <overriding-bundle-templates >`:
66+ put them in the ``app/Resources/TwigBundle/views/Exception/ `` directory.
67+
68+ A typical project that returns HTML and JSON pages, might look like this:
69+
70+ .. code-block :: text
71+
72+ app/
73+ └─ Resources/
74+ └─ TwigBundle/
75+ └─ views/
76+ └─ Exception/
77+ ├─ error404.html.twig
78+ ├─ error403.html.twig
79+ ├─ error.html.twig # All other HTML errors (including 500)
80+ ├─ error404.json.twig
81+ ├─ error403.json.twig
82+ ├─ error.json.twig # All other JSON errors (including 500)
83+
84+ Example 404 Error Template
85+ --------------------------
86+
87+ To override the 404 error template for HTML pages, create a new
7688``error404.html.twig `` template located at ``app/Resources/TwigBundle/views/Exception/ ``:
7789
7890.. code-block :: html+jinja
@@ -83,16 +95,17 @@ For example, to override the 404 error template for HTML pages, create a new
8395 {% block body %}
8496 <h1>Page not found</h1>
8597
98+ {# example security usage, see below #}
99+ {% if app.user and is_granted('IS_AUTHENTICATED_FULLY') %}
100+ {# ... #}
101+ {% endif %}
102+
86103 <p>
87104 The requested page couldn't be located. Checkout for any URL
88105 misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
89106 </p>
90107 {% endblock %}
91108
92- Commonly, Symfony applications redefine the ``error404.html.twig `` template, the
93- ``error500.html.twig `` template for internal server errors and the generic
94- ``error.html.twig `` template to catch any other error different from 404 and 500.
95-
96109In case you need them, the ``ExceptionController `` passes some information to
97110the error template via the ``status_code `` and ``status_text `` variables that
98111store the HTTP status code and message respectively.
@@ -132,14 +145,9 @@ is undefined. The solution is to add the following check before using this funct
132145 Testing Error Pages during Development
133146~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134147
135- One of the biggest hurdles of testing how do custom error pages look in your
136- application is the fact that Symfony ignores them in the development environment
137- and displays the default exception pages instead.
138-
139- You may be tempted to set the ``kernel.debug `` parameter to ``false `` to disable
140- the debug mode in the development environment. However, this practice is not
141- recommended because it will also stop Symfony from recompiling your Twig templates,
142- among many other things.
148+ While you're in the development environment, Symfony shows the big *exception *
149+ page instead of your shiny new customized error page. So, how can you see
150+ what it looks like and debug it?
143151
144152The recommended solution is to use a third-party bundle called `WebfactoryExceptionsBundle `_.
145153This bundle provides a special test controller that allows you to easily display
0 commit comments