merged branch KingCrunch/feature/static-template-cache (PR #6083)

This PR was squashed before being merged into the master branch (closes #6083).

Commits
-------

6236c18 [FrameworkBundle] Added caching to TemplateController

Discussion
----------

[FrameworkBundle] Added caching to TemplateController

Because the main purpose for the `TemplateController` seems to be to render static pages like "disclaimer" and such, it seems useful to allow caching.

    imprint:
        pattern: /imprint
        defaults:
            _controller: SymfonyFrameworkBundle:Template:template
            template: "::pages/imprint.html.twig"
            maxAge: 86400

---------------------------------------------------------------------------

by pierredup at 2012-11-21T20:24:53Z

IMHO I think the caching should be allowed to be set optionally

---------------------------------------------------------------------------

by KingCrunch at 2012-11-21T20:38:54Z

I wrote it this way, because I assume, that it will cover more use-cases, than the other way round, but you are right, that this will change the current behaviour. Would like to hear other opinions, because I don't think one uses this action for anything else than fully-static content (means: The current behaviour doesn't feel very useful to me).

---------------------------------------------------------------------------

by pierredup at 2012-11-21T20:48:19Z

I totally agree, but I would then suggest keep the caching on by default, but have the option to turn it off if necessary

---------------------------------------------------------------------------

by pierredup at 2012-11-21T20:52:01Z

Actually I think to have caching permanently enabled for static content would probably be the best scenario, but I like to think in terms of flexibility and specific user requirements. It would be great to get some opinions from others on this

---------------------------------------------------------------------------

by KingCrunch at 2012-11-23T21:12:45Z

I thought about it and I come to the conclusion, that it is probably a not so good idea to enable caching by default, because ... well, it's not possible to disable it again. I guess something like this

    {{ render '@AcmeBundle:ArticleController:latest' with {count: 1} }}

may be not so uncommon as I suggested in the first commit.

---------------------------------------------------------------------------

by fabpot at 2012-12-03T22:18:51Z

Can you make a PR for the docs? (symfony/symfony-docs). Thanks.
This commit is contained in:
Fabien Potencier 2012-12-06 09:57:25 +01:00
commit ee346bbc22

View File

@ -25,11 +25,28 @@ class TemplateController extends ContainerAware
* Renders a template.
*
* @param string $template The template name
* @param int|null $maxAge Max age for client caching
* @param int|null $sharedAge Max age for shared (proxy) caching
* @param Boolean|null $private Whether or not caching should apply for client caches only
*
* @return Response A Response instance
*/
public function templateAction($template)
public function templateAction($template, $maxAge = null, $sharedAge = null, $private = null)
{
return $this->container->get('templating')->renderResponse($template);
/** @var $response \Symfony\Component\HttpFoundation\Response */
$response = $this->container->get('templating')->renderResponse($template);
if ($maxAge) {
$response->setMaxAge($maxAge);
}
if ($sharedAge) {
$response->setSharedMaxAge($sharedAge);
}
if ($private) {
$response->setPrivate();
} else if ($private === false || (is_null($private) && ($maxAge || $sharedAge))) {
$response->setPublic($private);
}
return $response;
}
}