@@ -9,14 +9,6 @@ In Symfony applications, all errors are treated as exceptions, no matter if they
9
9
are just a 404 Not Found error or a fatal error triggered by throwing some
10
10
exception in your code.
11
11
12
- If your app has the `TwigBundle `_ installed, a special controller handles these
13
- exceptions. This controller displays debug information for errors and allows to
14
- customize error pages, so run this command to make sure the bundle is installed:
15
-
16
- .. code-block :: terminal
17
-
18
- $ composer require twig
19
-
20
12
In the :ref: `development environment <configuration-environments >`,
21
13
Symfony catches all the exceptions and displays a special **exception page **
22
14
with lots of debug information to help you discover the root problem:
@@ -39,45 +31,50 @@ Error pages for the production environment can be customized in different ways
39
31
depending on your needs:
40
32
41
33
#. If you just want to change the contents and styles of the error pages to match
42
- the rest of your application, :ref: `override the default error templates <use-default-exception-controller >`;
34
+ the rest of your application, :ref: `override the default error templates <use-default-error-controller >`;
35
+
36
+ #. If you want to change the contents of non-HTML error output, :ref: `create a new normalizer <overriding-non-html-error-output >`;
43
37
44
38
#. If you also want to tweak the logic used by Symfony to generate error pages,
45
- :ref: `override the default exception controller <custom-exception -controller >`;
39
+ :ref: `override the default error controller <custom-error -controller >`;
46
40
47
41
#. If you need total control of exception handling to execute your own logic
48
42
:ref: `use the kernel.exception event <use-kernel-exception-event >`.
49
43
50
- .. _use-default-exception -controller :
51
- .. _using-the-default-exceptioncontroller :
44
+ .. _use-default-error -controller :
45
+ .. _using-the-default-errorcontroller :
52
46
53
47
Overriding the Default Error Templates
54
48
--------------------------------------
55
49
56
- When the error page loads, an internal :class: `Symfony\\ Bundle\\ TwigBundle\\ Controller\\ ExceptionController `
50
+ You can use the built-in Twig error renderer to easily override the default error templates.
51
+ Both the TwigBundle and TwigBridge need to be installed for this.
52
+ Run this command to ensure both are installed:
53
+
54
+ .. code-block :: terminal
55
+
56
+ $ composer require twig
57
+
58
+ When the error page loads, :class: `Symfony\\ Bridge\\ Twig\\ ErrorRenderer\\ TwigErrorRenderer `
57
59
is used to render a Twig template to show the user.
58
60
59
61
.. _controller-error-pages-by-status-code :
60
62
61
- This controller uses the HTTP status code, the request format and the following
63
+ This renderer uses the HTTP status code and the following
62
64
logic to determine the template filename:
63
65
64
- #. Look for a template for the given format and status code (like ``error404.json.twig ``
65
- or ``error500.html.twig ``);
66
+ #. Look for a template for the given status code (like ``error500.html.twig ``);
66
67
67
68
#. If the previous template doesn't exist, discard the status code and look for
68
- a generic template for the given format (like ``error.json.twig `` or
69
- ``error.xml.twig ``);
70
-
71
- #. If none of the previous templates exist, fall back to the generic HTML template
72
- (``error.html.twig ``).
69
+ a generic error template (``error.html.twig ``).
73
70
74
71
.. _overriding-or-adding-templates :
75
72
76
73
To override these templates, rely on the standard Symfony method for
77
74
:ref: `overriding templates that live inside a bundle <override-templates >` and
78
75
put them in the ``templates/bundles/TwigBundle/Exception/ `` directory.
79
76
80
- A typical project that returns HTML and JSON pages might look like this:
77
+ A typical project that returns HTML pages might look like this:
81
78
82
79
.. code-block :: text
83
80
@@ -87,10 +84,7 @@ A typical project that returns HTML and JSON pages might look like this:
87
84
└─ Exception/
88
85
├─ error404.html.twig
89
86
├─ error403.html.twig
90
- ├─ error.html.twig # All other HTML errors (including 500)
91
- ├─ error404.json.twig
92
- ├─ error403.json.twig
93
- └─ error.json.twig # All other JSON errors (including 500)
87
+ └─ error.html.twig # All other HTML errors (including 500)
94
88
95
89
Example 404 Error Template
96
90
--------------------------
@@ -112,24 +106,17 @@ To override the 404 error template for HTML pages, create a new
112
106
</p>
113
107
{% endblock %}
114
108
115
- In case you need them, the ``ExceptionController `` passes some information to
109
+ In case you need them, the ``TwigErrorRenderer `` passes some information to
116
110
the error template via the ``status_code `` and ``status_text `` variables that
117
111
store the HTTP status code and message respectively.
118
112
119
113
.. tip ::
120
114
121
- You can customize the status code by implementing
115
+ You can customize the status code of an exception by implementing
122
116
:class: `Symfony\\ Component\\ HttpKernel\\ Exception\\ HttpExceptionInterface `
123
117
and its required ``getStatusCode() `` method. Otherwise, the ``status_code ``
124
118
will default to ``500 ``.
125
119
126
- .. note ::
127
-
128
- The exception pages shown in the development environment can be customized
129
- in the same way as error pages. Create a new ``exception.html.twig `` template
130
- for the standard HTML exception page or ``exception.json.twig `` for the JSON
131
- exception page.
132
-
133
120
Security & 404 Pages
134
121
--------------------
135
122
@@ -146,12 +133,12 @@ While you're in the development environment, Symfony shows the big *exception*
146
133
page instead of your shiny new customized error page. So, how can you see
147
134
what it looks like and debug it?
148
135
149
- Fortunately, the default ``ExceptionController `` allows you to preview your
136
+ Fortunately, the default ``ErrorController `` allows you to preview your
150
137
*error * pages during development.
151
138
152
- To use this feature, you need to load some special routes provided by TwigBundle
139
+ To use this feature, you need to load some special routes provided by FrameworkBundle
153
140
(if the application uses :ref: `Symfony Flex <symfony-flex >` they are loaded
154
- automatically when installing Twig support ):
141
+ automatically when installing `` symfony/framework-bundle `` ):
155
142
156
143
.. configuration-block ::
157
144
@@ -193,56 +180,88 @@ for a given status code as HTML or for a given status code and format.
193
180
http://localhost/index.php/_error/{statusCode}
194
181
http://localhost/index.php/_error/{statusCode}.{format}
195
182
196
- .. _custom-exception-controller :
197
- .. _replacing-the-default-exceptioncontroller :
183
+ .. _overriding-non-html-error-output :
184
+
185
+ Overriding Error output for non-HTML formats
186
+ --------------------------------------------
198
187
199
- Overriding the Default ExceptionController
200
- ------------------------------------------
188
+ To override non-HTML error output, the Serializer component needs to be installed.
189
+
190
+ .. code-block :: terminal
191
+
192
+ $ composer require serializer
193
+
194
+ The Serializer component has a built-in ``FlattenException `` normalizer (``ProblemNormalizer ``) and JSON/XML/CSV/YAML
195
+ encoders by default. That means that if an exception were to be thrown in your application, Symfony can output it in
196
+ a format supported by one of the encoders. If you want to change how the output is structured, all you have to do
197
+ is create a new Normalizer that supports the ``FlattenException `` input::
198
+
199
+ class MyCustomProblemNormalizer implements NormalizerInterface
200
+ {
201
+ public function normalize($exception, $format = null, array $context = [])
202
+ {
203
+ return [
204
+ 'content': 'This is my custom problem normalizer.',
205
+ 'exception': [
206
+ 'message': $exception->getMessage(),
207
+ 'code': $exception->getStatusCode(),
208
+ ],
209
+ ];
210
+ }
211
+
212
+ public function supportsNormalization($data, $format = null)
213
+ {
214
+ return $data instanceof FlattenException;
215
+ }
216
+ }
217
+
218
+ .. _custom-error-controller :
219
+ .. _replacing-the-default-errorcontroller :
220
+
221
+ Overriding the Default ErrorController
222
+ --------------------------------------
201
223
202
224
If you need a little more flexibility beyond just overriding the template,
203
225
then you can change the controller that renders the error page. For example,
204
226
you might need to pass some additional variables into your template.
205
227
206
228
To do this, create a new controller anywhere in your application and set
207
- the :ref: `twig.exception_controller <config-twig-exception-controller >`
229
+ the :ref: `framework.error_controller <config-framework-error_controller >`
208
230
configuration option to point to it:
209
231
210
232
.. configuration-block ::
211
233
212
234
.. code-block :: yaml
213
235
214
- # config/packages/twig .yaml
215
- twig :
216
- exception_controller : App\Controller\ExceptionController ::showAction
236
+ # config/packages/framework .yaml
237
+ framework :
238
+ error_controller : App\Controller\ErrorController ::showAction
217
239
218
240
.. code-block :: xml
219
241
220
- <!-- config/packages/twig .xml -->
242
+ <!-- config/packages/framework .xml -->
221
243
<?xml version =" 1.0" encoding =" UTF-8" ?>
222
244
<container xmlns =" http://symfony.com/schema/dic/services"
223
245
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
224
- xmlns : twig =" http://symfony.com/schema/dic/twig"
225
246
xsi : schemaLocation =" http://symfony.com/schema/dic/services
226
- https://symfony.com/schema/dic/services/services-1.0.xsd
227
- http://symfony.com/schema/dic/twig
228
- https://symfony.com/schema/dic/twig/twig-1.0.xsd" >
247
+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
229
248
230
- <twig : config >
231
- <twig : exception-controller >App\Controller\ExceptionController ::showAction</twig : exception-controller >
232
- </twig : config >
249
+ <framework : config >
250
+ <framework : error_controller >App\Controller\ErrorController ::showAction</framework : error_controller >
251
+ </framework : config >
233
252
234
253
</container >
235
254
236
255
.. code-block :: php
237
256
238
257
// config/packages/twig.php
239
- $container->loadFromExtension('twig ', [
240
- 'exception_controller ' => 'App\Controller\ExceptionController ::showAction',
258
+ $container->loadFromExtension('framework ', [
259
+ 'error_controller ' => 'App\Controller\ErrorController ::showAction',
241
260
// ...
242
261
]);
243
262
244
- The :class: `Symfony\\ Component\\ HttpKernel\\ EventListener\\ ExceptionListener `
245
- class used by the TwigBundle as a listener of the ``kernel.exception `` event creates
263
+ The :class: `Symfony\\ Component\\ HttpKernel\\ EventListener\\ ErrorListener `
264
+ class used by the FrameworkBundle as a listener of the ``kernel.exception `` event creates
246
265
the request that will be dispatched to your controller. In addition, your controller
247
266
will be passed two parameters:
248
267
@@ -355,5 +374,3 @@ time and again, you can have just one (or several) listeners deal with them.
355
374
your application (like :class: `Symfony\\ Component\\ Security\\ Core\\ Exception\\ AccessDeniedException `)
356
375
and takes measures like redirecting the user to the login page, logging them
357
376
out and other things.
358
-
359
- .. _`TwigBundle` : https://github.com/symfony/twig-bundle
0 commit comments