Last active
July 26, 2018 15:29
-
-
Save alexander-schranz/b81c0f8f54ce04a00559 to your computer and use it in GitHub Desktop.
Render twig template with sulu parameters and set a default cache lifetime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller\Website; | |
use Sulu\Bundle\HttpCacheBundle\Cache\HttpCache; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Response; | |
abstract class AbstractWebsiteController extends Controller | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function render($view, array $parameters = [], Response $response = null) | |
{ | |
if (!$response) { | |
$response = $this->createResponse(); | |
} | |
return parent::render( | |
$view, | |
$this->get('sulu_website.resolver.template_attribute')->resolve($parameters), | |
$response | |
); | |
} | |
/** | |
* Create a response with a default cacheLifeTIme of 24h. | |
* | |
* @param int $cacheLifeTime | |
* | |
* @return Response | |
*/ | |
protected function createResponse($cacheLifeTime = 86400) | |
{ | |
$response = new Response(); | |
if ($cacheLifeTime) { | |
$response->setPublic(); | |
$response->setMaxAge($this->getParameter('sulu_http_cache.cache.shared_max_age')); | |
$response->setSharedMaxAge($this->getParameter('sulu_http_cache.cache.max_age')); | |
$response->headers->set( | |
HttpCache::HEADER_REVERSE_PROXY_TTL, | |
$cacheLifeTime | |
); | |
} else { | |
$response->setPrivate(); | |
} | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment