@@ -155,3 +155,98 @@ action:
155
155
Because you are redirecting to a route instead of a path, the required
156
156
option is called ``route `` in the ``redirect() `` action, instead of ``path ``
157
157
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
+ <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
+ )));
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