在 Symfony2 中重定向和阻止缓存 [英] Redirecting and preventing cache in Symfony2
问题描述
我正在这样做:
domain.com/route-name/?do-something=1
..它设置了一个 cookie,然后使用 302 重定向重定向到这个:
..which sets a cookie and then redirects to this using a 302 redirect:
domain.com/route-name/
它允许在不考虑页面查看的情况下执行操作(cookie 为用户存储设置).
It allows an action to take place regardless of the page viewing (cookie stores a setting for the user).
我使用默认的 Symfony2 反向代理缓存,一切正常,但我需要阻止上述两个请求缓存.
I am using the default Symfony2 reverse-proxy cache and all is well, but I need to prevent both the above requests from caching.
我正在使用它来执行重定向:
I am using this to perform the redirect:
// $event being a listener, usually a request listener
$response = new RedirectResponse($url, 302);
$this->event->setResponse($response);
我尝试过这样的事情,但似乎没有任何效果:
I've tried things like this but nothing seems to work:
$response->setCache(array('max_age' => 0));
header("Cache-Control: no-cache");
那么我如何阻止它缓存这些页面?
So how do I stop it caching those pages?
推荐答案
您必须确保使用 RedirectResponse(如果设置了 GET 参数)以及针对路由的常规 Response 发送以下标头:>
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;
像这样实现你想要的:
$response->setPrivate();
$response->setMaxAge(0);
$response->setSharedMaxAge(0);
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('no-store', true);
在昏迷的答案中,private 很重要,但没有提及.
private is important and missing in coma's answer.
不同之处在于使用 Cache-Control: private 您不允许代理缓存通过它们的数据.
The difference is that with Cache-Control: private you are not allowing proxies to cache the data that travels through them.
这篇关于在 Symfony2 中重定向和阻止缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!