Skip to content

Commit 463f05f

Browse files
committed
[FrameworkBundle][Routing] Added new template and redirect routes configuration
1 parent 651b5f6 commit 463f05f

File tree

2 files changed

+81
-83
lines changed

2 files changed

+81
-83
lines changed

routing.rst

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,8 @@ in the main article about Symfony templates.
12921292
Redirecting to URLs and Routes Directly from a Route
12931293
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12941294

1295-
Use the ``RedirectController`` to redirect to other routes and URLs:
1295+
Use the :class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController`
1296+
to redirect to other routes and URLs:
12961297

12971298
.. configuration-block::
12981299

@@ -1301,28 +1302,27 @@ Use the ``RedirectController`` to redirect to other routes and URLs:
13011302
# config/routes.yaml
13021303
doc_shortcut:
13031304
path: /doc
1304-
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController
1305+
redirect_to_route: 'doc_page'
1306+
1307+
# redirections are temporary by default (code 302) but you can make them permanent (code 301)
1308+
permanent: true
1309+
# add this to keep the original query string parameters when redirecting
1310+
keepQueryParams: true
1311+
# add this to keep the HTTP method when redirecting. The redirect status changes
1312+
# * for temporary redirects, it uses the 307 status code instead of 302
1313+
# * for permanent redirects, it uses the 308 status code instead of 301
1314+
keepRequestMethod: true
1315+
1316+
# optionally you can define some arguments passed to the route
13051317
defaults:
1306-
route: 'doc_page'
1307-
# optionally you can define some arguments passed to the route
13081318
page: 'index'
13091319
version: 'current'
1310-
# redirections are temporary by default (code 302) but you can make them permanent (code 301)
1311-
permanent: true
1312-
# add this to keep the original query string parameters when redirecting
1313-
keepQueryParams: true
1314-
# add this to keep the HTTP method when redirecting. The redirect status changes
1315-
# * for temporary redirects, it uses the 307 status code instead of 302
1316-
# * for permanent redirects, it uses the 308 status code instead of 301
1317-
keepRequestMethod: true
13181320
13191321
legacy_doc:
13201322
path: /legacy/doc
1321-
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController
1322-
defaults:
1323-
# this value can be an absolute path or an absolute URL
1324-
path: 'https://legacy.example.com/doc'
1325-
permanent: true
1323+
# this value can be an absolute path or an absolute URL
1324+
redirect_to_url: 'https://legacy.example.com/doc'
1325+
permanent: true
13261326
13271327
.. code-block:: xml
13281328
@@ -1333,68 +1333,71 @@ Use the ``RedirectController`` to redirect to other routes and URLs:
13331333
xsi:schemaLocation="http://symfony.com/schema/routing
13341334
https://symfony.com/schema/routing/routing-1.0.xsd">
13351335
1336-
<route id="doc_shortcut" path="/doc"
1337-
controller="Symfony\Bundle\FrameworkBundle\Controller\RedirectController">
1338-
<default key="route">doc_page</default>
1336+
<redirect-route id="doc_shortcut" path="/doc"
1337+
redirect-to-route="doc_page"
1338+
<!-- redirections are temporary by default (code 302) but you can make them permanent (code 301)-->
1339+
permanent="true">
13391340
<!-- optionally you can define some arguments passed to the route -->
13401341
<default key="page">index</default>
13411342
<default key="version">current</default>
1342-
<!-- redirections are temporary by default (code 302) but you can make them permanent (code 301)-->
1343-
<default key="permanent">true</default>
13441343
<!-- add this to keep the original query string parameters when redirecting -->
13451344
<default key="keepQueryParams">true</default>
13461345
<!-- add this to keep the HTTP method when redirecting. The redirect status changes:
13471346
* for temporary redirects, it uses the 307 status code instead of 302
13481347
* for permanent redirects, it uses the 308 status code instead of 301 -->
13491348
<default key="keepRequestMethod">true</default>
1350-
</route>
1349+
</redirect-route>
13511350
1352-
<route id="legacy_doc" path="/legacy/doc"
1353-
controller="Symfony\Bundle\FrameworkBundle\Controller\RedirectController">
1351+
<url-redirect-route id="legacy_doc" path="/legacy/doc"
13541352
<!-- this value can be an absolute path or an absolute URL -->
1355-
<default key="path">https://legacy.example.com/doc</default>
1353+
redirect-to-url="https://legacy.example.com/doc"
13561354
<!-- redirections are temporary by default (code 302) but you can make them permanent (code 301)-->
1357-
<default key="permanent">true</default>
1358-
</route>
1355+
permanent="true">
1356+
</url-redirect-route>
13591357
</routes>
13601358
13611359
.. code-block:: php
13621360
13631361
// config/routes.php
13641362
use App\Controller\DefaultController;
1365-
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
13661363
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
13671364
13681365
return function (RoutingConfigurator $routes) {
13691366
$routes->add('doc_shortcut', '/doc')
1370-
->controller(RedirectController::class)
1371-
->defaults([
1372-
'route' => 'doc_page',
1373-
// optionally you can define some arguments passed to the template
1367+
->redirectToRoute('doc_page')
1368+
1369+
// redirections are temporary by default (code 302) but you can make them permanent (code 301)
1370+
->permanent()
1371+
1372+
// add this to keep the original query string parameters when redirecting
1373+
->keepQueryParams()
1374+
1375+
// add this to keep the HTTP method when redirecting. The redirect status changes:
1376+
// * for temporary redirects, it uses the 307 status code instead of 302
1377+
// * for permanent redirects, it uses the 308 status code instead of 301
1378+
->keepRequestMethod()
1379+
1380+
// optionally you can define some arguments passed to the template
1381+
->defaults([
13741382
'page' => 'index',
13751383
'version' => 'current',
1376-
// redirections are temporary by default (code 302) but you can make them permanent (code 301)
1377-
'permanent' => true,
1378-
// add this to keep the original query string parameters when redirecting
1379-
'keepQueryParams' => true,
1380-
// add this to keep the HTTP method when redirecting. The redirect status changes:
1381-
// * for temporary redirects, it uses the 307 status code instead of 302
1382-
// * for permanent redirects, it uses the 308 status code instead of 301
1383-
'keepRequestMethod' => true,
13841384
])
13851385
;
13861386
13871387
$routes->add('legacy_doc', '/legacy/doc')
1388-
->controller(RedirectController::class)
1389-
->defaults([
1390-
// this value can be an absolute path or an absolute URL
1391-
'path' => 'https://legacy.example.com/doc',
1392-
// redirections are temporary by default (code 302) but you can make them permanent (code 301)
1393-
'permanent' => true,
1394-
])
1388+
// this value can be an absolute path or an absolute URL
1389+
->redirectToUrl('https://legacy.example.com/doc')
1390+
1391+
// redirections are temporary by default (code 302) but you can make them permanent (code 301)
1392+
->permanent()
13951393
;
13961394
};
13971395
1396+
.. versionadded:: 5.1
1397+
1398+
This short syntax was introduced in Symfony 5.1. Before you had to
1399+
define the controller and specific route attributes using ``defaults``.
1400+
13981401
.. tip::
13991402

14001403
Symfony also provides some utilities to

templates.rst

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,9 @@ Rendering a Template Directly from a Route
457457
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
458458

459459
Although templates are usually rendered in controllers and services, you can
460-
render static pages that don't need any variables directly from the route
461-
definition. Use the special ``TemplateController`` provided by Symfony:
460+
render static pages from the route definition. Use the special
461+
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\TemplateController`
462+
provided by Symfony:
462463

463464
.. configuration-block::
464465

@@ -467,19 +468,17 @@ definition. Use the special ``TemplateController`` provided by Symfony:
467468
# config/routes.yaml
468469
acme_privacy:
469470
path: /privacy
470-
controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController
471-
defaults:
472-
# the path of the template to render
473-
template: 'static/privacy.html.twig'
471+
# the path of the template to render
472+
template: 'static/privacy.html.twig'
474473
475-
# special options defined by Symfony to set the page cache
476-
maxAge: 86400
477-
sharedAge: 86400
474+
# special options defined by Symfony to set the page cache
475+
maxAge: 86400
476+
sharedAge: 86400
478477
479-
# optionally you can define some arguments passed to the template
480-
context:
481-
site_name: 'ACME'
482-
theme: 'dark'
478+
# some variables passed to the template
479+
context:
480+
site_name: 'ACME'
481+
theme: 'dark'
483482
484483
.. code-block:: xml
485484
@@ -489,50 +488,46 @@ definition. Use the special ``TemplateController`` provided by Symfony:
489488
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
490489
xsi:schemaLocation="http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd">
491490
492-
<route id="acme_privacy"
491+
<template-route id="acme_privacy"
493492
path="/privacy"
494-
controller="Symfony\Bundle\FrameworkBundle\Controller\TemplateController">
495493
<!-- the path of the template to render -->
496-
<default key="template">static/privacy.html.twig</default>
494+
template="static/privacy.html.twig"
497495
498496
<!-- special options defined by Symfony to set the page cache -->
499-
<default key="maxAge">86400</default>
500-
<default key="sharedAge">86400</default>
497+
maxAge="86400"
498+
sharedMaxAge="86400">
501499
502-
<!-- optionally you can define some arguments passed to the template -->
500+
<!-- some variables passed to the template -->
503501
<default key="context">
504502
<default key="site_name">ACME</default>
505503
<default key="theme">dark</default>
506504
</default>
507-
</route>
505+
</template-route>
508506
</routes>
509507
510508
.. code-block:: php
511509
512510
// config/routes.php
513-
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
514511
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
515512
516513
return function (RoutingConfigurator $routes) {
517514
$routes->add('acme_privacy', '/privacy')
518-
->controller(TemplateController::class)
519-
->defaults([
520-
// the path of the template to render
521-
'template' => 'static/privacy.html.twig',
522-
523-
// special options defined by Symfony to set the page cache
524-
'maxAge' => 86400,
525-
'sharedAge' => 86400,
526-
527-
// optionally you can define some arguments passed to the template
528-
'context' => [
529-
'site_name' => 'ACME',
530-
'theme' => 'dark',
531-
]
515+
// the path of the template to render and a context of variables passed to it
516+
->template('static/privacy.html.twig', [
517+
'site_name' => 'ACME',
518+
'theme' => 'dark',
532519
])
520+
// special options defined by Symfony to set the page cache
521+
->maxAge(86400)
522+
->sharedMaxAge(86400)
533523
;
534524
};
535525
526+
.. versionadded:: 5.1
527+
528+
This short syntax was introduced in Symfony 5.1. Before you had to
529+
define the controller and specific route attributes using ``defaults``.
530+
536531
.. versionadded:: 5.1
537532

538533
The ``context`` option was introduced in Symfony 5.1.

0 commit comments

Comments
 (0)