7

我正在这样做:

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

..它设置一个cookie,然后使用302重定向重定向到这个:

domain.com/route-name/

无论页面查看如何,它都允许执行操作(cookie 存储用户的设置)。

我正在使用默认的 Symfony2 反向代理缓存,一切都很好,但我需要防止上述两个请求都被缓存。

我正在使用它来执行重定向:

// $event being a listener, usually a request listener

$response = new RedirectResponse($url, 302);    
$this->event->setResponse($response);

我已经尝试过这样的事情,但似乎没有任何效果:

$response->setCache(array('max_age' => 0));
header("Cache-Control: no-cache");

那么如何停止缓存这些页面呢?

4

2 回答 2

13

您必须确保发送以下带有 RedirectResponse 的标头(如果设置了 GET 参数)和您的常规 Response 路由:

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);

私人是重要的,在昏迷的答案中缺失。

不同之处在于使用 Cache-Control: private 您不允许代理缓存通过它们的数据。

于 2013-05-25T19:39:55.157 回答
1

试试你的回复:

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

您也可以使用注释:

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

看看:

为什么在 HTTP 响应中应该同时使用 no-cache 和 no-store?

于 2013-05-25T19:39:24.930 回答