@@ -27,7 +27,7 @@ Error pages for the production environment can be customized in different ways
27
27
depending on your needs:
28
28
29
29
#. 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 >`;
31
31
32
32
#. If you also want to tweak the logic used by Symfony to generate error pages,
33
33
:ref: `override the default exception controller <custom-exception-controller >`;
@@ -41,25 +41,16 @@ depending on your needs:
41
41
Overriding the Default Error Templates
42
42
--------------------------------------
43
43
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.
55
46
56
47
.. _cookbook-error-pages-by-status-code :
57
48
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 :
60
51
61
52
#. 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 ``);
63
54
64
55
#. If the previous template doesn't exist, discard the status code and look for
65
56
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:
71
62
.. _overriding-or-adding-templates :
72
63
73
64
To 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
76
88
``error404.html.twig `` template located at ``app/Resources/TwigBundle/views/Exception/ ``:
77
89
78
90
.. code-block :: html+jinja
@@ -83,16 +95,17 @@ For example, to override the 404 error template for HTML pages, create a new
83
95
{% block body %}
84
96
<h1>Page not found</h1>
85
97
98
+ {# example security usage, see below #}
99
+ {% if app.user and is_granted('IS_AUTHENTICATED_FULLY') %}
100
+ {# ... #}
101
+ {% endif %}
102
+
86
103
<p>
87
104
The requested page couldn't be located. Checkout for any URL
88
105
misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
89
106
</p>
90
107
{% endblock %}
91
108
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
-
96
109
In case you need them, the ``ExceptionController `` passes some information to
97
110
the error template via the ``status_code `` and ``status_text `` variables that
98
111
store 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
132
145
Testing Error Pages during Development
133
146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134
147
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?
143
151
144
152
The recommended solution is to use a third-party bundle called `WebfactoryExceptionsBundle `_.
145
153
This bundle provides a special test controller that allows you to easily display
0 commit comments