Skip to content

Commit 442f259

Browse files
author
Jules Pietri
committed
[FrameworkBundle][Routing] Added doc for new "template", "redirect" shortcuts
1 parent 0c1aa7d commit 442f259

File tree

2 files changed

+104
-51
lines changed

2 files changed

+104
-51
lines changed

routing/redirect_in_config.rst

+67-36
Original file line numberDiff line numberDiff line change
@@ -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+
86107
In this example, you configured a route for the ``/`` path and let the
87108
``RedirectController`` redirect it to ``/app``. The ``permanent`` switch
88109
tells 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+
};

templating/render_without_controller.rst

+37-15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ can do this without creating a controller:
2727
template: static/privacy.html.twig
2828
methods: GET
2929
30+
# or
31+
acme_privacy:
32+
path: /privacy
33+
template: static/privacy.html.twig
34+
methods: GET
35+
3036
.. code-block:: xml
3137
3238
<!-- config/routes.xml -->
@@ -41,12 +47,18 @@ can do this without creating a controller:
4147
methods="GET">
4248
<default key="template">static/privacy.html.twig</default>
4349
</route>
50+
51+
<!-- or -->
52+
<route id="acme_privacy"
53+
path="/privacy"
54+
template="static/privacy.html.twig"
55+
methods="GET" />
4456
</routes>
4557
4658
.. code-block:: php
4759
4860
// config/routes.php
49-
namespace Symfony\Component\Routing\Loader\Configurator;
61+
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
5062
5163
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
5264
@@ -58,6 +70,12 @@ can do this without creating a controller:
5870
'template' => 'static/privacy.html.twig',
5971
])
6072
;
73+
74+
// or
75+
$routes->add('acme_privacy', '/privacy')
76+
->template('static/privacy.html.twig')
77+
->methods(['GET'])
78+
;
6179
};
6280
6381
The ``TemplateController`` will render whatever template you've passed as the
@@ -89,11 +107,10 @@ exactly how your page is cached:
89107
# config/routes.yaml
90108
acme_privacy:
91109
path: /privacy
92-
controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController
110+
template: 'static/privacy.html.twig'
93111
defaults:
94-
template: 'static/privacy.html.twig'
95112
maxAge: 86400
96-
sharedAge: 86400
113+
sharedMaxAge: 86400
97114
methods: GET
98115
99116
.. code-block:: xml
@@ -106,37 +123,42 @@ exactly how your page is cached:
106123
107124
<route id="acme_privacy"
108125
path="/privacy"
109-
controller="Symfony\Bundle\FrameworkBundle\Controller\TemplateController"
126+
template="static/privacy.html.twig"
110127
methods="GET">
111-
<default key="template">static/privacy.html.twig</default>
112128
<default key="maxAge">86400</default>
113-
<default key="sharedAge">86400</default>
129+
<default key="sharedMaxAge">86400</default>
114130
</route>
115131
</routes>
116132
117133
.. code-block:: php
118134
119135
// config/routes.php
120-
namespace Symfony\Component\Routing\Loader\Configurator;
136+
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
121137
122138
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
123139
124140
return function (RoutingConfigurator $routes) {
125141
$routes->add('acme_privacy', '/privacy')
126-
->controller(TemplateController::class)
142+
->template('static/privacy.html.twig')
127143
->methods(['GET'])
128-
->defaults([
129-
'template' => 'static/privacy.html.twig',
130-
'maxAge' => 86400,
131-
'sharedAge' => 86400,
132-
])
144+
->maxAge(86400)
145+
->sharedMaxAge(86400)
133146
;
134147
};
135148
149+
.. versionadded:: 4.3
150+
151+
The ``template`` shortcut has been introduced in Symfony 4.3
152+
136153
The ``maxAge`` and ``sharedAge`` values are used to modify the Response
137154
object created in the controller. For more information on caching, see
138155
:doc:`/http_cache`.
139156

140157
There is also a ``private`` variable (not shown here). By default, the Response
141-
will be made public, as long as ``maxAge`` or ``sharedAge`` are passed.
158+
will be made public, as long as ``maxAge`` or ``sharedAge`` (or
159+
``sharedMaxAge``) are passed.
142160
If set to ``true``, the Response will be marked as private.
161+
162+
.. versionadded:: 4.3
163+
164+
The ``sharedMaxAge`` variable has been introduced in Symfony 4.3

0 commit comments

Comments
 (0)