@@ -230,7 +230,7 @@ Consider the following routing configuration:
230
230
<routes xmlns =" http://symfony.com/schema/routing"
231
231
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
232
232
xsi : schemaLocation =" http://symfony.com/schema/routing
233
- https://symfony.com/schema/routing/routing-1.0.xsd" >
233
+ https://symfony.com/schema/routing/framework- routing-1.0.xsd" >
234
234
235
235
<route id =" blog_index"
236
236
path =" /"
@@ -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,81 +468,77 @@ 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
- # whether or not caching should apply for client caches only
481
- private : true
478
+ # whether or not caching should apply for client caches only
479
+ private : true
482
480
483
- # optionally you can define some arguments passed to the template
484
- context :
485
- site_name : ' ACME'
486
- theme : ' dark'
481
+ # some variables passed to the template
482
+ context :
483
+ site_name : ' ACME'
484
+ theme : ' dark'
487
485
488
486
.. code-block :: xml
489
487
490
488
<!-- config/routes.xml -->
491
489
<?xml version =" 1.0" encoding =" UTF-8" ?>
492
490
<routes xmlns =" http://symfony.com/schema/routing"
493
491
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
494
- xsi : schemaLocation =" http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd" >
492
+ xsi : schemaLocation =" http://symfony.com/schema/routing
493
+ https://symfony.com/schema/routing/framework-routing-1.0.xsd" >
495
494
496
- <route id =" acme_privacy"
495
+ <template- route id =" acme_privacy"
497
496
path =" /privacy"
498
- controller =" Symfony\Bundle\FrameworkBundle\Controller\TemplateController" >
499
497
<!-- the path of the template to render -->
500
- < default key = " template " > static/privacy.html.twig</ default >
498
+ template=" static/privacy.html.twig"
501
499
502
500
<!-- special options defined by Symfony to set the page cache -->
503
- < default key = " maxAge " > 86400</ default >
504
- < default key = " sharedAge " > 86400</ default >
501
+ maxAge=" 86400"
502
+ sharedMaxAge=" 86400" >
505
503
506
504
<!-- whether or not caching should apply for client caches only -->
507
505
<default key =" private" >true</default >
508
506
509
- <!-- optionally you can define some arguments passed to the template -->
510
- <default key = " context" >
511
- <default key =" site_name" >ACME</default >
512
- <default key =" theme" >dark</default >
513
- </default >
514
- </route >
507
+ <!-- some variables passed to the template -->
508
+ <context >
509
+ <string key =" site_name" >ACME</string >
510
+ <string key =" theme" >dark</string >
511
+ </context >
512
+ </template- route >
515
513
</routes >
516
514
517
515
.. code-block :: php
518
516
519
517
// config/routes.php
520
- use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
521
- use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
518
+ use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RoutingConfigurator;
522
519
523
520
return function (RoutingConfigurator $routes) {
524
521
$routes->add('acme_privacy', '/privacy')
525
- ->controller(TemplateController::class)
526
- ->defaults([
527
- // the path of the template to render
528
- 'template' => 'static/privacy.html.twig',
529
-
530
- // special options defined by Symfony to set the page cache
531
- 'maxAge' => 86400,
532
- 'sharedAge' => 86400,
533
-
534
- // whether or not caching should apply for client caches only
535
- 'private' => true,
536
-
537
- // optionally you can define some arguments passed to the template
538
- 'context' => [
539
- 'site_name' => 'ACME',
540
- 'theme' => 'dark',
541
- ]
522
+ // the path of the template to render and a context of variables passed to it
523
+ ->template('static/privacy.html.twig', [
524
+ 'site_name' => 'ACME',
525
+ 'theme' => 'dark',
542
526
])
527
+
528
+ // special options defined by Symfony to set the page cache
529
+ ->maxAge(86400)
530
+ ->sharedMaxAge(86400)
531
+
532
+ // whether or not caching should apply for client caches only
533
+ ->private()
543
534
;
544
535
};
545
536
537
+ .. versionadded :: 5.1
538
+
539
+ This short syntax was introduced in Symfony 5.1. Before you had to
540
+ define the controller and specific route attributes using ``defaults ``.
541
+
546
542
.. versionadded :: 5.1
547
543
548
544
The ``context `` option was introduced in Symfony 5.1.
0 commit comments