Redirecting and preventing cache in Symfony2

后端 未结 2 1469
我寻月下人不归
我寻月下人不归 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: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.

提交回复
热议问题