Contributed by
Sebastian Krebs
in #6083.
All websites have some kind of static pages like an about page; a static page being a page that does not need any logic to be rendered. As there is no logic, creating an empty controller for them is simple but cumbersome:
1 2 3 4 5 6 7 8 9 10 11
public function aboutAction()
{
return $this->render('AcmeBundle:Pages:about.html.twig');
}
public function locationAction()
{
return $this->render('AcmeBundle:Pages:location.html.twig');
}
// ...
And then, you need to also reference them in the routing configuration file:
1 2
about: { pattern: /about, defaults: { _controller: 'AcmeBundle:Pages:about' } }
location: { pattern: /location, defaults: { _controller: 'AcmeBundle:Pages:location' } }
... and even if using annotations simplifies things a bit, that is still too much work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**
* @Route("/about", name="about")
* @Template
*/
public function aboutAction()
{
return array();
}
/**
* @Route("/location", name="location")
* @Template
*/
public function locationAction()
{
return array();
}
// ...
Instead, you can use the built-in FrameworkBundle:Template:template
controller and configure everything from the routing configuration file
directly:
1 2
about: { pattern: /about, defaults: { _controller: FrameworkBundle:Template:template, template: 'AcmeBundle:Pages:about.html.twig' } }
location: { pattern: /location, defaults: { _controller: FrameworkBundle:Template:template, template: 'AcmeBundle:Pages:location.html.twig' } }
In this configuration, we associate a URL to a template via the
FrameworkBundle:Template:template
controller. This has been working since
2.0, but as of 2.2, you can also set the caching strategy:
1 2 3 4 5 6 7 8
about:
pattern: /about
defaults:
_controller: FrameworkBundle:Template:template
template: 'AcmeBundle:Pages:about.html.twig'
maxAge: 86400
sharedMaxAge: 86400
private: false
great!
Good
Nice ! it reduces the number of lines of codes for just having a static page. In addition, they are cached by default. No need to do that manually :)
thanx
I "discovered" this a while ago, it really deserves a proper place in the documentation https://coderwall.com/p/pj1opa
great ! a good practice !
making things easier, good job
I forgot to mention that there is a PR to add this to the documentation:
https://github.com/symfony/symfony-docs/pull/2002
Didn't know about that feature at all, nice highlight and nice addition!
Wow, great feature!
Amazing Routing !!! HTTP Cache Control Powa
Nice. FrameworkBundle:Template:template is new to me, too.
great!
Great new info, But if this template extends ::base.html.twig that contain username logged in user, will it cache all html output as seen by user in their browser, or just content from this template ?
Thanks
great, waiting for 2.2!
wow, that was something that annoyed me for a long time too and this solution is just awesome!
Good !
Symfony2 great, always helping to make life easier
super -)
I have never noticed that! Documentation is a must for such things. I would use it a while ago. Great feature!
Cool feature!