No cache header with annotation

To configure a response without a cache in the controller, you can do this:

$response = new Response(); $result = $this->renderView( 'AcmeDemoBundle:Default:index.html.twig', array('products' => $products, 'form' => $form->createView())); $response->headers->addCacheControlDirective('no-cache', true); $response->headers->addCacheControlDirective('max-age', 0); $response->headers->addCacheControlDirective('must-revalidate', true); $response->headers->addCacheControlDirective('no-store', true); $response->setContent($result); return $response; 

But using annotations to ensure that each method has the same result, how can you do this?

I tried this, but keep saving the cache, and if I use the browser back button, the cache is saved:

 /** * @Cache(maxage="0", vary="no-cache, must-revalidate, no-store", smaxage="0", expires="now", public="false") */ class DefaultController extends Controller { /** * Homepage: show products * * @Route("/", name="homepage") * @Template */ public function indexAction() { $sessionCart = $this->get('demo'); $filters = $sessionCart->getFilters($this->getDoctrine()->getEntityManager()); $products = $this->getDoctrine()->getRepository('AcmeDemoBundle:Product')->search($filters); $form = $this->createForm(new FilterType, $filters); return array('products' => $products, 'form' => $form->createView()); } 

If it is indicated that the documentation states:

 @Cache(vary=["no-cache", "must-revalidate", "no-store"]... 

gives a syntax error that does not expect "[", so I tried as above.

+4
source share
2 answers

You mix two things. In the first snippet, you set the cache headers, but with annotation you want to set the Vary header. But Vary completely different from the Cache-Control header, which should have no-cache, must-revalidate, no-store . Vary means what the request thinks (i.e. cookies), the answer may differ. See this answer for understanding: fooobar.com/questions/68754 / ...

In your case (without a cache), you can rely on the default values that Symfony sets if there are no cache headers:

Symfony2 automatically sets a reasonable and conservative Cache-Control header if the developer has not set any of them by following these rules:

  • If the cache header is not defined ( Cache-Control , Expires , ETag or Last-Modified ), Cache-Control set to no-cache , which means that the response will not be cached;

EDIT: if you need to set a cache header for each controller action, you can work with the kernel.response event. Create a listener that responds to this event and modifies the response with appropriate cache controls.

 namespace Acme\DemoBundle\EventListener; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; class AcmeCacheListener { public function onKernelResponse(FilterResponseEvent $event) { $response = $event->getResponse(); $response->headers->addCacheControlDirective('no-cache', true); $response->headers->addCacheControlDirective('max-age', 0); $response->headers->addCacheControlDirective('must-revalidate', true); $response->headers->addCacheControlDirective('no-store', true); } } 

and in services.yml

 services: kernel.listener.your_listener_name: class: Acme\DemoBundle\EventListener\AcmeCacheListener tags: - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse } 
+9
source

I assume the main problem is that in the Cache annotation there is no option for no-cache, no-store and must-revalidate. I can’t explain why. In addition to Patrick's answer, if you have multiple controllers and you want to apply headers to only one, you can check the controller class inside the listener, for example:

 class AcmeCacheListener { public function onKernelResponse(FilterResponseEvent $event) { $controller = $event->getRequest()->attributes->get('_controller'); $requiredController = "Acme\Controller\DefaultController"; if( substr($controller, 0,strlen($requiredController)) == $requiredController) { $response = $event->getResponse(); $response->headers->addCacheControlDirective('no-cache', true); $response->headers->addCacheControlDirective('max-age', 0); $response->headers->addCacheControlDirective('must-revalidate', true); $response->headers->addCacheControlDirective('no-store', true); } } } 
0
source

Source: https://habr.com/ru/post/1492163/


All Articles