8000 minor #6857 Update events.rst (tabbi89) · symfony/symfony-docs@bc83735 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc83735

Browse files
committed
minor #6857 Update events.rst (tabbi89)
This PR was merged into the 2.7 branch. Discussion ---------- Update events.rst What I have done here is just a complete explanation what is going on during setting response status code. Sentence `setting the status on the response won't work` is not true because if we pass "proper status code" it will be passed to final response without further checks - condition: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/HttpKernel.php#L248. So for example If I do something like this ``` php // ... $exception = $event->getException(); $response = new Response(); // setup the Response object based on the caught exception $event->setResponse($response); $event->setStatusCode(400); ``` Finally 400 status code will be passed to response! In my opinion current documentation is incorrect and as a developer I would like to have valid information. Maybe what I wrote is too precise maybe a reference to code would be a better option ? Thanks a lot. Commits ------- 4db1863 Update events.rst
2 parents fd3b1d7 + 4db1863 commit bc83735

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

reference/events.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,8 @@ and set a new ``Exception`` object, or do nothing::
241241

242242
.. note::
243243

244-
As Symfony ensures that the Response status code is set to the most
245-
appropriate one depending on the exception, setting the status on the
246-
response won't work. If you want to overwrite the status code (which you
247-
should not without a good reason), set the ``X-Status-Code`` header::
244+
If you want to overwrite the status code (which you should not without a good reason),
245+
set the ``X-Status-Code`` header::
248246

249247
$response = new Response(
250248
'Error',
@@ -254,6 +252,20 @@ and set a new ``Exception`` object, or do nothing::
254252
)
255253
);
256254

255+
This indeed will skip all checks that Symfony processes on setting response status code.
256+
First check that Symfony does is fitting response status code.
257+
Symfony leaves set status code as it is if it belongs to one of group:
258+
259+
:method:`Symfony\\Component\\HttpFoundation\\Response::isClientError`
260+
261+
:method:`Symfony\\Component\\HttpFoundation\\Response::isServerError`
262+
263+
:method:`Symfony\\Component\\HttpFoundation\\Response::isRedirect`
264+
265+
If status code is different then Symfony checks instance of raised Exception if it is
266+
:class:`Symfony\\Component\\HttpKernel\\Exception\\HttpExceptionInterface` then its headers including status code are passed to response.
267+
Otherwise status code 500 is set to response.
268+
257269
.. seealso::
258270

259271
Read more on the :ref:`kernel.exception event <component-http-kernel-kernel-exception>`.

0 commit comments

Comments
 (0)
0