Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9468bf9

Browse filesBrowse files
Reword and added an example
I did some rewords based on https://symfony.com/blog/new-in-symfony-4-1-307-and-308-redirections and added a full config example.
1 parent c8ce65d commit 9468bf9
Copy full SHA for 9468bf9

File tree

1 file changed

+93
-17
lines changed
Filter options

1 file changed

+93
-17
lines changed

‎routing/redirect_in_config.rst

Copy file name to clipboardExpand all lines: routing/redirect_in_config.rst
+93-17Lines changed: 93 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,97 @@ action:
156156
option is called ``route`` in the ``redirect()`` action, instead of ``path``
157157
in the ``urlRedirect()`` action.
158158

159-
Redirecting POST/PUT calls
160-
--------------------------
161-
162-
As a default behaviour of both methods mentioned above results in sending
163-
response with ``301`` or ``302`` HTTP status codes, the following call will
164-
be made with use of HTTP request method. But in some scenarios it's either
165-
expected or required that the following call will be made with the same HTTP
166-
method, i.e. when initial call was ``POST`` one, then following one should
167-
be also ``POST`` not ``GET``. In order to achieve this both
168-
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction`
169-
and
170-
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::redirectAction`
171-
are accepting aditional switch called ``keepRequestMethod``.
159+
Keeping the Request Method when Redirecting
160+
-------------------------------------------
161+
162+
The redirections performed in the previous examples use the ``301`` and ``302``
163+
HTTP status codes. For legacy reasons, these HTTP redirections change the method
164+
of ``POST`` requests to ``GET`` (because redirecting a ``POST`` request didn't
165+
work well in old browsers).
166+
167+
However, in some scenarios it's either expected or required that the redirection
168+
request uses the same HTTP method. That's why the HTTP standard defines two
169+
additional status codes (``307`` and ``308``) to perform temporary/permanent
170+
redirects that maintain the original request method.
171+
172+
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction`
173+
and :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::redirectAction`
174+
methods accept an additional argument called ``keepRequestMethod``. When it's
175+
set to ``true``, temporary redirects use ``307`` code instead of ``302`` and
176+
permanent redirects use ``308`` code instead of ``301``::
177+
178+
.. configuration-block::
179+
180+
.. code-block:: yaml
181+
182+
# config/routes.yaml
183+
184+
# redirects with the 308 status code
185+
route_foo:
186+
# ...
187+
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction
188+
defaults:
189+
# ...
190+
permanent: true
191+
keepRequestMethod: true
192+
193+
# redirects with the 307 status code
194+
route_bar:
195+
# ...
196+
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction
197+
defaults:
198+
# ...
199+
permanent: false
200+
keepRequestMethod: true
201+
202+
.. code-block:: xml
203+
204+
<!-- config/routes.xml -->
205+
<?xml version="1.0" encoding="UTF-8" ?>
206+
<routes xmlns="http://symfony.com/schema/routing"
207+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
208+
xsi:schemaLocation="http://symfony.com/schema/routing
209+
http://symfony.com/schema/routing/routing-1.0.xsd">
210+
211+
<!-- redirects with the 308 status code -->
212+
<route id="route_foo" path="...">
213+
<!-- ... -->
214+
<default key="_controller">Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction</default>
215+
<default key="permanent">true</default>
216+
<default key="keepRequestMethod">true</default>
217+
</route>
218+
219+
<!-- redirects with the 307 status code -->
220+
<route id="route_bar" path="...">
221+
<!-- ... -->
222+
<default key="_controller">Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction</default>
223+
<default key="permanent">false</default>
224+
<default key="keepRequestMethod">true</default>
225+
</route>
226+
</routes>
227+
228+
.. code-block:: php
229+
230+
// config/routes.php
231+
use Symfony\Component\Routing\RouteCollection;
232+
use Symfony\Component\Routing\Route;
233+
234+
$collection = new RouteCollection();
235+
236+
// redirects with the 308 status code
237+
$collection->add('route_foo', new Route('...', array(
238+
// ...
239+
'_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
240+
'permanent' => true,
241+
'keepRequestMethod' => true,
242+
)));
172243
173-
When ``keepRequestMethod`` is set to ``true`` with either ``permanent`` set to
174-
``false`` which will lead to a ``307`` response or ``308`` with
175-
``permanent`` being ``true``. Theses codes will give information in the HTTP
176-
request that the method should be repeated without altering the body.
244+
// redirects with the 307 status code
245+
$collection->add('route_bar', new Route('...', array(
246+
// ...
247+
'_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
248+
'permanent' => false,
249+
'keepRequestMethod' => true,
250+
)));
251+
252+
return $collection;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.