Symfony HTTP Cache-Control header 覆盖

Symfony HTTP Cache-Control header overriding

我之前回答过 问题。

我目前在 Symfony 3.4 中的代码。

 $response->setCache([
     'max_age' => 3600,
     'public' => true,
     's_maxage' => 3600,
 ]);


 // (optional) set a custom Cache-Control directive
 $response->headers->addCacheControlDirective('must-revalidate', true);

 return $response;

出于某种原因,我仍然得到 Cache-Control 和其他 headers 像这样:

Cache-Control: max-age=0, must-revalidate, private, s-maxage=3600
X-Symfony-Cache: GET /: miss

我注意到在 Symfony 3.2 中代码运行良好,但在 3.4 中我没有在 Cache-Control 中得到 public(并且 max-age=0 看起来也很糟糕)。

第二件事是在控制器中转储 $response - 一切看起来都很好:

#headers: array:2 [▼
  "cache-control" => array:1 [▼
    0 => "max-age=3600, must-revalidate, public, s-maxage=3600"
  ]
  "date" => array:1 [▼
    0 => "Sat, 12 May 2018 18:42:04 GMT"
  ]
]

这是某种 Symfony 错误吗?我应该举报吗?

与 Symfony 3.4.14 相同。花了5个小时才发现 由于 3.4 版本 symfony 覆盖 Cache-Control header 如果 session 存在 (4.1 documentation).

Symfony 3.4 文档没有此信息。

在我的例子中,我使用了默认配置的 symfony/security-bundle:

security:
    providers:
     ....
    firewalls:
        main:
          anonymous: true

此代码初始化一个会话。您可能想使用:

security:
    providers:
     ....
    firewalls:
        main:
          security: false