Redirecting and preventing cache in Symfony2

后端 未结 2 1463
我寻月下人不归
我寻月下人不归 2020-12-31 22:02

I am doing this:

domain.com/route-name/?do-something=1

..which sets a cookie and then redirects to this using a 302 redirect:



        
相关标签:
2条回答
  • 2020-12-31 22:15

    Try this on your response:

    $response->headers->addCacheControlDirective('no-cache', true);
    $response->headers->addCacheControlDirective('max-age', 0);
    $response->headers->addCacheControlDirective('must-revalidate', true);
    $response->headers->addCacheControlDirective('no-store', true);
    

    You can use annotations too:

    http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/cache.html

    And take a look at:

    Why both no-cache and no-store should be used in HTTP response?

    0 讨论(0)
  • 2020-12-31 22:31

    You have to make sure you are sending the following headers with the RedirectResponse ( if the GET parameter is set ) AND with your regular Response for the route:

    Cache-Control: private, max-age=0, must-revalidate, no-store;
    

    Achieve what you want like this:

    $response->setPrivate();
    $response->setMaxAge(0);
    $response->setSharedMaxAge(0);
    $response->headers->addCacheControlDirective('must-revalidate', true);
    $response->headers->addCacheControlDirective('no-store', true);
    

    private is important and missing in coma's answer.

    The difference is that with Cache-Control: private you are not allowing proxies to cache the data that travels through them.

    0 讨论(0)
提交回复
热议问题