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