@@ -245,7 +245,7 @@ Consider the following routing configuration:
245
245
246
246
// config/routes.php
247
247
use App\Controller\BlogController;
248
- use Symfony\Component \Routing\Loader\Configurator\RoutingConfigurator;
248
+ use Symfony\Bundle\FrameworkBundle \Routing\Loader\Configurator\RoutingConfigurator;
249
249
250
250
return function (RoutingConfigurator $routes) {
251
251
$routes->add('blog_index', '/')
@@ -457,8 +457,8 @@ Rendering a Template Directly from a Route
457
457
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
458
458
459
459
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 :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ TemplateController `
460
+ render static pages from the route definition. Use the special
461
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ TemplateController `
462
462
provided by Symfony:
463
463
464
464
.. configuration-block ::
@@ -468,19 +468,17 @@ provided by Symfony:
468
468
# config/routes.yaml
469
469
acme_privacy :
470
470
path : /privacy
471
- controller : Symfony\Bundle\FrameworkBundle\Controller\TemplateController
472
- defaults :
473
- # the path of the template to render
474
- template : ' static/privacy.html.twig'
471
+ # the path of the template to render
472
+ template : ' static/privacy.html.twig'
475
473
476
- # special options defined by Symfony to set the page cache
477
- maxAge : 86400
478
- sharedAge : 86400
474
+ # special options defined by Symfony to set the page cache
475
+ maxAge : 86400
476
+ sharedAge : 86400
479
477
480
- # optionally you can define some arguments passed to the template
481
- context :
482
- site_name : ' ACME'
483
- theme : ' dark'
478
+ # some variables passed to the template
479
+ context :
480
+ site_name : ' ACME'
481
+ theme : ' dark'
484
482
485
483
.. code-block :: xml
486
484
@@ -490,50 +488,46 @@ provided by Symfony:
490
488
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
491
489
xsi : schemaLocation =" http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd" >
492
490
493
- <route id =" acme_privacy"
491
+ <template- route id =" acme_privacy"
494
492
path =" /privacy"
495
- controller =" Symfony\Bundle\FrameworkBundle\Controller\TemplateController" >
496
493
<!-- the path of the template to render -->
497
- < default key = " template " > static/privacy.html.twig</ default >
494
+ template=" static/privacy.html.twig"
498
495
499
496
<!-- special options defined by Symfony to set the page cache -->
500
- < default key = " maxAge " > 86400</ default >
501
- < default key = " sharedAge " > 86400</ default >
497
+ maxAge=" 86400"
498
+ sharedMaxAge=" 86400" >
502
499
503
- <!-- optionally you can define some arguments passed to the template -->
504
- <default key = " context" >
505
- <default key =" site_name" >ACME</default >
506
- <default key =" theme" >dark</default >
500
+ <!-- some variables passed to the template -->
501
+ <context >
502
+ <string key =" site_name" >ACME</string >
503
+ <string key =" theme" >dark</string >
507
504
</default >
508
- </route >
505
+ </template- route >
509
506
</routes >
510
507
511
508
.. code-block :: php
512
509
513
510
// config/routes.php
514
- use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
515
- use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
511
+ use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RoutingConfigurator;
516
512
517
513
return function (RoutingConfigurator $routes) {
518
514
$routes->add('acme_privacy', '/privacy')
519
- ->controller(TemplateController::class)
520
- ->defaults([
521
- // the path of the template to render
522
- 'template' => 'static/privacy.html.twig',
523
-
524
- // special options defined by Symfony to set the page cache
525
- 'maxAge' => 86400,
526
- 'sharedAge' => 86400,
527
-
528
- // optionally you can define some arguments passed to the template
529
- 'context' => [
530
- 'site_name' => 'ACME',
531
- 'theme' => 'dark',
532
- ]
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',
533
519
])
520
+ // special options defined by Symfony to set the page cache
521
+ ->maxAge(86400)
522
+ ->sharedMaxAge(86400)
534
523
;
535
524
};
536
525
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
+
537
531
.. versionadded :: 5.1
538
532
539
533
The ``context `` option was introduced in Symfony 5.1.
0 commit comments