Archives


Master Symfony2 fundamentals

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

Fabien Potencier
New in Symfony 2.2: Cache support for static pages
by Fabien Potencier – December 11, 2012 – 20 comments

Sebastian Krebs

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

Comments RSS

  • Marc Weistroff
    #1 Marc Weistroff said on the 2012/12/11 at 16:06
    great!
  • Mario J. Inga Cahuana
    #2 Mario J. Inga Cahuana said on the 2012/12/11 at 16:14
    Good
  • Skander MABROUK
    #3 Skander MABROUK said on the 2012/12/11 at 16:22
    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
  • Matteo Giordano
    #4 Matteo Giordano said on the 2012/12/11 at 16:23
    I "discovered" this a while ago, it _really_ deserves a proper place in the documentation https://coderwall.com/p/pj1opa
  • Belgacem TLILI
    #5 Belgacem TLILI said on the 2012/12/11 at 16:31
    great !
    a good practice !
  • Tito Miguel Costa
    #6 Tito Miguel Costa said on the 2012/12/11 at 16:35
    making things easier, good job
  • Fabien Potencier
    #7 Fabien Potencier said on the 2012/12/11 at 16:44
    I forgot to mention that there is a PR to add this to the documentation:

    https://github.com/symfony/symfony-docs/pull/2002
  • Chris Sedlmayr
    #8 Chris Sedlmayr said on the 2012/12/11 at 16:45
    Didn't know about that feature at all, nice highlight and nice addition!
  • Jean-François Côté
    #9 Jean-François Côté said on the 2012/12/11 at 17:05
    Wow, great feature!
  • Symfomany
    #10 Symfomany said on the 2012/12/11 at 20:46
    Amazing Routing !!! HTTP Cache Control Powa
  • Thomas Luzat
    #11 Thomas Luzat said on the 2012/12/11 at 21:26
    Nice. FrameworkBundle:Template:template is new to me, too.
  • David Badura
    #12 David Badura said on the 2012/12/11 at 22:34
    great!
  • Ardian Yuli Setyanto
    #13 Ardian Yuli Setyanto said on the 2012/12/12 at 07:47
    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
  • Piotr Antosik
    #14 Piotr Antosik said on the 2012/12/12 at 09:23
    great, waiting for 2.2!
  • Ben Bieker
    #15 Ben Bieker said on the 2012/12/12 at 09:41
    wow, that was something that annoyed me for a long time too and this solution is just awesome!
  • Marco Leong
    #16 Marco Leong said on the 2012/12/12 at 09:51
    Good !
  • Yosvel Quintero Arguelles
    #17 Yosvel Quintero Arguelles said on the 2012/12/12 at 17:53
    Symfony2 great,
    always helping to make life easier
  • pinouf
    #18 pinouf said on the 2012/12/12 at 23:39
    super -)
  • Michael Klik
    #19 Michael Klik said on the 2012/12/17 at 22:05
    I have never noticed that! Documentation is a must for such things. I would use it a while ago. Great feature!
  • Víctor Mateo
    #20 Víctor Mateo said on the 2013/01/14 at 23:23
    Cool feature!