@@ -40,6 +40,13 @@ action to redirect to this new url:
40
40
path : /app
41
41
permanent : true
42
42
43
+ # or
44
+ homepage :
45
+ path : /
46
+ url_redirect : /app
47
+ defaults :
48
+ permanent : true
49
+
43
50
.. code-block :: xml
44
51
45
52
<!-- config/routes.xml -->
@@ -59,6 +66,11 @@ action to redirect to this new url:
59
66
<default key =" path" >/app</default >
60
67
<default key =" permanent" >true</default >
61
68
</route >
69
+
70
+ <!-- or -->
71
+ <route id =" homepage" path =" /" url-redirect =" path" >/app">
72
+ <default key =" permanent" >true</default >
73
+ </route >
62
74
</routes >
63
75
64
76
.. code-block :: php
@@ -81,8 +93,17 @@ action to redirect to this new url:
81
93
'permanent' => true,
82
94
])
83
95
;
96
+
97
+ // or
98
+ $routes->urlRedirect('homepage', '/', '/app)
99
+ ->permanent(true)
100
+ ;
84
101
};
85
102
103
+ .. versionadded :: 4.3
104
+
105
+ The "url redirect" shortcut has been introduced in Symfony 4.3.
106
+
86
107
In this example, you configured a route for the ``/ `` path and let the
87
108
``RedirectController `` redirect it to ``/app ``. The ``permanent `` switch
88
109
tells the action to issue a ``301 `` HTTP status code instead of the default
@@ -115,6 +136,14 @@ action:
115
136
# ...and keep the original query string parameters
116
137
keepQueryParams : true
117
138
139
+ # or
140
+ admin :
141
+ path : /wp-admin
142
+ redirect : sonata_admin_dashboard
143
+ defaults :
144
+ permanent : true
145
+ keepQueryParams : true
146
+
118
147
.. code-block :: xml
119
148
120
149
<!-- config/routes.xml -->
@@ -135,6 +164,12 @@ action:
135
164
<!-- ...and keep the original query string parameters -->
136
165
<default key =" keepQueryParams" >true</default >
137
166
</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 >
138
173
</routes >
139
174
140
175
.. code-block :: php
@@ -156,8 +191,18 @@ action:
156
191
'keepQueryParams' => true,
157
192
])
158
193
;
194
+
195
+ // or
196
+ $routes->redirect('admin', '/wp-admin', 'sonata_admin_dashboard')
197
+ ->permanent(true)
198
+ ->keepQueryParams(true)
199
+ ;
159
200
};
160
201
202
+ .. versionadded :: 4.3
203
+
204
+ The ``redirect `` shortcut has been introduced in Symfony 4.3.
205
+
161
206
.. caution ::
162
207
163
208
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``:
190
235
# config/routes.yaml
191
236
192
237
# 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
196
241
defaults :
197
- # ...
198
242
permanent : true
199
243
keepRequestMethod : true
200
244
201
245
# redirects with the 307 status code
202
246
route_bar :
203
- # ...
204
- controller : Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction
247
+ path : /bar
248
+ url_redirect : /tmp/bar
205
249
defaults :
206
250
# ...
207
251
permanent : false
@@ -217,19 +261,13 @@ permanent redirects use ``308`` code instead of ``301``:
217
261
http://symfony.com/schema/routing/routing-1.0.xsd" >
218
262
219
263
<!-- 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" >
224
265
<default key =" permanent" >true</default >
225
266
<default key =" keepRequestMethod" >true</default >
226
267
</route >
227
268
228
269
<!-- 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" >
233
271
<default key =" permanent" >false</default >
234
272
<default key =" keepRequestMethod" >true</default >
235
273
</route >
@@ -238,25 +276,18 @@ permanent redirects use ``308`` code instead of ``301``:
238
276
.. code-block :: php
239
277
240
278
// 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