Php Symfony 2 Symfony\Component\HttpFoundation HeaderBag headers

Php Symfony 2 Symfony\Component\HttpFoundation HeaderBag headers,php,symfony,Php,Symfony,因此,在Symfony2中,我需要执行以下操作: $response->headers->addCacheControlDirective(); $response->headers->addCacheControlDirective(); $response->headers->addCacheControlDirective(); $response->headers->addCacheControlDirective(); $kernel

因此,在Symfony2中,我需要执行以下操作:

$response->headers->addCacheControlDirective();
$response->headers->addCacheControlDirective();
$response->headers->addCacheControlDirective();
$response->headers->addCacheControlDirective();
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);

$response->headers->addCacheControlDirective('no-cache', true);
$response->headers->addCacheControlDirective('max-age', 0);
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('no-store', true);
上述方法包括:

namespace Symfony\Component\HttpFoundation;

class HeaderBag
我通过更新我的app_dev.php和app.php并添加以下内容使其正常工作:

$response->headers->addCacheControlDirective();
$response->headers->addCacheControlDirective();
$response->headers->addCacheControlDirective();
$response->headers->addCacheControlDirective();
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);

$response->headers->addCacheControlDirective('no-cache', true);
$response->headers->addCacheControlDirective('max-age', 0);
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('no-store', true);
这是修改的标题的正确方法吗?我应该做些其他事情吗


Symfony是个新手,我想用正确的方式做每件事。

您的解决方案会奏效,但这不是最佳实践。Symfony2的强大功能之一是基于事件的,IMO更好的方法是在kernel.response事件上编写一个侦听器来更改头。此外,例如,通过这种方式,您可以检查您的路线


可以为找到一个hipster实现,也可以看看他的服务。

罗马有很多方法,如果你想让所有请求/响应都使用该标题,那么你的解决方案可能可以正常工作。是的,它可以工作,只是不知道我是否需要扩展任何内容或以某种奇特的hipster方式进行。你看过http缓存章节吗。我想你可能会在那里找到你的时髦方法。如果答案好的话,你能同意吗?我已经同意你的答案:)谢谢,伙计。