Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit61856a2

Browse files
committed
minor#9298 [FrameworkBundle] PR #26213 Document redirections with 307/308 HTTP status codes (ZipoKing, javiereguiluz)
This PR was merged into the master branch.Discussion----------[FrameworkBundle] PR #26213 Document redirections with 307/308 HTTP status codesThis documents changes introduced with pull requestsymfony/symfony#26213Commits-------9468bf9 Reword and added an examplec8ce65d Changes after@Simperfit reviewb3984d0 [PR #26213 Document redirections with 307/308 HTTP status codes
2 parentse8f9334 +9468bf9 commit61856a2

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

‎routing/redirect_in_config.rst‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,98 @@ action:
155155
Because you are redirecting to a route instead of a path, the required
156156
option is called ``route`` in the ``redirect()`` action, instead of ``path``
157157
in the ``urlRedirect()`` action.
158+
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+
<routesxmlns="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+
<routeid="route_foo"path="...">
213+
<!-- ...-->
214+
<defaultkey="_controller">Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction</default>
215+
<defaultkey="permanent">true</default>
216+
<defaultkey="keepRequestMethod">true</default>
217+
</route>
218+
219+
<!-- redirects with the 307 status code-->
220+
<routeid="route_bar"path="...">
221+
<!-- ...-->
222+
<defaultkey="_controller">Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction</default>
223+
<defaultkey="permanent">false</default>
224+
<defaultkey="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+
)));
243+
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)

[8]ページ先頭

©2009-2025 Movatter.jp