[英]Redirecting and preventing cache in Symfony2
I am doing this: 我正在这样做:
domain.com/route-name/?do-something=1
..which sets a cookie and then redirects to this using a 302 redirect: ..它设置一个cookie,然后使用302重定向重定向到该cookie:
domain.com/route-name/
It allows an action to take place regardless of the page viewing (cookie stores a setting for the user). 它允许执行任何操作,而与浏览页面无关(Cookie为用户存储设置)。
I am using the default Symfony2 reverse-proxy cache and all is well, but I need to prevent both the above requests from caching. 我正在使用默认的Symfony2反向代理缓存,一切都很好,但是我需要防止上述两个请求都缓存。
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? 那么,如何停止它缓存那些页面呢?
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: 您必须确保使用RedirectResponse(如果设置了GET参数)以及常规路由响应发送以下标头:
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. 区别在于,使用Cache-Control:private不允许代理代理缓存通过它们的数据。
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 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? 为什么在HTTP响应中应同时使用无缓存和无存储?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.