New in Symfony 4.1: 307 and 308 redirections
February 22, 2018 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Paweł Smoliński
in #26213.
In web applications is common to use the 301
HTTP status code to make
permanent redirections and the 302
code to make temporary redirections.
However, a problem with these redirection types is that a POST
request is
transformed into a GET
request when doing the redirection for legacy
reasons (redirecting a POST
request didn't work well in old browsers).
In order to solve these issues, the HTTP standard introduced two new status codes:
307
(Temporary Redirect): defined in RFC 7231 and similar to302
(Found), except that it does not allow changing the request method fromPOST
toGET
.308
(Permanent Redirect): defined in RFC 7538 and similar to301
(Moved Permanently), except that it does not allow changing the request method fromPOST
toGET
.
In Symfony 4.1 we added support for these new redirection types by adding a new
keepRequestMethod
argument to the redirectAction()
and
urlRedirectAction()
methods of the RedirectController
. You can also use
it as the keepRequestMethod
option in the route definitions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
route_301:
# ...
defaults:
# ...
permanent: true
route_302:
# ...
defaults:
# ...
permanent: false
route_307:
# ...
defaults:
# ...
permanent: false
keepRequestMethod: true
route_308:
# ...
defaults:
# ...
permanent: true
keepRequestMethod: true
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.