@@ -40,6 +40,13 @@ action to redirect to this new url:
4040 path : /app
4141 permanent : true
4242
43+ # or
44+ homepage :
45+ path : /
46+ url_redirect : /app
47+ defaults :
48+ permanent : true
49+
4350 .. code-block :: xml
4451
4552 <!-- config/routes.xml -->
@@ -59,6 +66,11 @@ action to redirect to this new url:
5966 <default key =" path" >/app</default >
6067 <default key =" permanent" >true</default >
6168 </route >
69+
70+ <!-- or -->
71+ <route id =" homepage" path =" /" url-redirect =" path" >/app">
72+ <default key =" permanent" >true</default >
73+ </route >
6274 </routes >
6375
6476 .. code-block :: php
@@ -81,8 +93,17 @@ action to redirect to this new url:
8193 'permanent' => true,
8294 ])
8395 ;
96+
97+ // or
98+ $routes->urlRedirect('homepage', '/', '/app)
99+ ->permanent(true)
100+ ;
84101 };
85102
103+ .. versionadded :: 4.3
104+
105+ The "url redirect" shortcut has been introduced in Symfony 4.3.
106+
86107In this example, you configured a route for the ``/ `` path and let the
87108``RedirectController `` redirect it to ``/app ``. The ``permanent `` switch
88109tells the action to issue a ``301 `` HTTP status code instead of the default
@@ -115,6 +136,14 @@ action:
115136 # ...and keep the original query string parameters
116137 keepQueryParams : true
117138
139+ # or
140+ admin :
141+ path : /wp-admin
142+ redirect : sonata_admin_dashboard
143+ defaults :
144+ permanent : true
145+ keepQueryParams : true
146+
118147 .. code-block :: xml
119148
120149 <!-- config/routes.xml -->
@@ -135,6 +164,12 @@ action:
135164 <!-- ...and keep the original query string parameters -->
136165 <default key =" keepQueryParams" >true</default >
137166 </route >
167+
168+ <!-- or -->
169+ <route id =" admin" path =" /wp-admin" redirect =" sonata_admin_dashboard" >
170+ <default key =" permanent" >true</default >
171+ <default key =" keepQueryParams" >true</default >
172+ </route >
138173 </routes >
139174
140175 .. code-block :: php
@@ -156,8 +191,18 @@ action:
156191 'keepQueryParams' => true,
157192 ])
158193 ;
194+
195+ // or
196+ $routes->redirect('admin', '/wp-admin', 'sonata_admin_dashboard')
197+ ->permanent(true)
198+ ->keepQueryParams(true)
199+ ;
159200 };
160201
202+ .. versionadded :: 4.3
203+
204+ The ``redirect `` shortcut has been introduced in Symfony 4.3.
205+
161206.. caution ::
162207
163208 Because you are redirecting to a route instead of a path, the required
@@ -190,18 +235,17 @@ permanent redirects use ``308`` code instead of ``301``:
190235 # config/routes.yaml
191236
192237 # redirects with the 308 status code
193- route_foo :
194- # ...
195- controller : Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction
238+ legacy_foo :
239+ path : /legacy/foo
240+ url_redirect : /foo
196241 defaults :
197- # ...
198242 permanent : true
199243 keepRequestMethod : true
200244
201245 # redirects with the 307 status code
202246 route_bar :
203- # ...
204- controller : Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction
247+ path : /bar
248+ url_redirect : /tmp/bar
205249 defaults :
206250 # ...
207251 permanent : false
@@ -217,19 +261,13 @@ permanent redirects use ``308`` code instead of ``301``:
217261 http://symfony.com/schema/routing/routing-1.0.xsd" >
218262
219263 <!-- redirects with the 308 status code -->
220- <route id =" route_foo"
221- path =" ..."
222- controller =" Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction" >
223- <!-- ... -->
264+ <route id =" legacy_foo" path =" /legacy/foo" url-redirect =" /foo" >
224265 <default key =" permanent" >true</default >
225266 <default key =" keepRequestMethod" >true</default >
226267 </route >
227268
228269 <!-- redirects with the 307 status code -->
229- <route id =" route_bar"
230- path =" ..."
231- controller =" Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction" >
232- <!-- ... -->
270+ <route id =" route_bar" path =" /bar" url-redirect =" /tmp/bar" >
233271 <default key =" permanent" >false</default >
234272 <default key =" keepRequestMethod" >true</default >
235273 </route >
@@ -238,25 +276,18 @@ permanent redirects use ``308`` code instead of ``301``:
238276 .. code-block :: php
239277
240278 // config/routes.php
241- use Symfony\Component\Routing\RouteCollection;
242- use Symfony\Component\Routing\Route;
243-
244- $collection = new RouteCollection();
245-
246- // redirects with the 308 status code
247- $collection->add('route_foo', new Route('...', [
248- // ...
249- '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
250- 'permanent' => true,
251- 'keepRequestMethod' => true,
252- ]));
253-
254- // redirects with the 307 status code
255- $collection->add('route_bar', new Route('...', [
256- // ...
257- '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
258- 'permanent' => false,
259- 'keepRequestMethod' => true,
260- ]));
261-
262- return $collection;
279+ namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
280+
281+ return function (RoutingConfigurator $routes) {
282+ // redirects with the 308 status code
283+ $routes->urlRedirect('legacy_foo', '/legacy/foo', '/foo')
284+ ->permanent(true)
285+ ->keepRequestMethod(true)
286+ ;
287+
288+ // redirects with the 307 status code
289+ $routes->urlRedirect('route_bar', '/bar', '/tpm/bar')
290+ ->permanent(false)
291+ ->keepRequestMethod(true)
292+ ;
293+ };
0 commit comments