@@ -245,7 +245,7 @@ Consider the following routing configuration:
245245
246246 // config/routes.php
247247 use App\Controller\BlogController;
248- use Symfony\Component \Routing\Loader\Configurator\RoutingConfigurator;
248+ use Symfony\Bundle\FrameworkBundle \Routing\Loader\Configurator\RoutingConfigurator;
249249
250250 return function (RoutingConfigurator $routes) {
251251 $routes->add('blog_index', '/')
@@ -457,8 +457,8 @@ Rendering a Template Directly from a Route
457457~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
458458
459459Although 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 `
462462provided by Symfony:
463463
464464.. configuration-block ::
@@ -468,72 +468,67 @@ provided by Symfony:
468468 # config/routes.yaml
469469 acme_privacy :
470470 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'
475473
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
479477
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'
484482
485483 .. code-block :: xml
486484
487485 <!-- config/routes.xml -->
488486 <?xml version =" 1.0" encoding =" UTF-8" ?>
489487 <routes xmlns =" http://symfony.com/schema/routing"
490488 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
491- xsi : schemaLocation =" http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd" >
489+ xsi : schemaLocation =" http://symfony.com/schema/routing
490+ https://symfony.com/schema/routing/framework-routing-1.0.xsd" >
492491
493- <route id =" acme_privacy"
492+ <template- route id =" acme_privacy"
494493 path =" /privacy"
495- controller =" Symfony\Bundle\FrameworkBundle\Controller\TemplateController" >
496494 <!-- the path of the template to render -->
497- < default key = " template " > static/privacy.html.twig</ default >
495+ template=" static/privacy.html.twig"
498496
499497 <!-- special options defined by Symfony to set the page cache -->
500- < default key = " maxAge " > 86400</ default >
501- < default key = " sharedAge " > 86400</ default >
502-
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 >
507- </default >
508- </route >
498+ maxAge=" 86400"
499+ sharedMaxAge=" 86400" >
500+
501+ <!-- some variables passed to the template -->
502+ <context >
503+ <string key =" site_name" >ACME</string >
504+ <string key =" theme" >dark</string >
505+ </context >
506+ </template- route >
509507 </routes >
510508
511509 .. code-block :: php
512510
513511 // config/routes.php
514- use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
515- use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
512+ use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RoutingConfigurator;
516513
517514 return function (RoutingConfigurator $routes) {
518515 $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- ]
516+ // the path of the template to render and a context of variables passed to it
517+ ->template('static/privacy.html.twig', [
518+ 'site_name' => 'ACME',
519+ 'theme' => 'dark',
533520 ])
521+ // special options defined by Symfony to set the page cache
522+ ->maxAge(86400)
523+ ->sharedMaxAge(86400)
534524 ;
535525 };
536526
527+ .. versionadded :: 5.1
528+
529+ This short syntax was introduced in Symfony 5.1. Before you had to
530+ define the controller and specific route attributes using ``defaults ``.
531+
537532.. versionadded :: 5.1
538533
539534 The ``context `` option was introduced in Symfony 5.1.
0 commit comments