Slim v3 重复 cache-control header

Slim v3 duplicates cache-control header

我必须 return 一个特定的 cache-control header (Cache-Control: public, max-stale=13910400) 但是当 运行 这个时,我得到这个:

Cache-control已经重复了,但我只需要自定义值。

$newResponse = $response->withHeader('Cache-Control', 'public, max-stale=13910400')->withJson($appInfo);
return $newResponse;

我试过了,但它不起作用(仅用于测试):

$newResponse = $response->withoutHeader('Cache-Control')->withHeader('Cache-Control', 'public, max-stale=13910400')->withJson($appInfo);
return $newResponse;

如何正确设置 header?

谢谢

我怀疑你可能有中间件问题。

您上面的代码确实产生了正确的输出。

$app->get('/test', function ($req, $res, $args) {
    header_remove("Cache-Control"); //Edit <--
    $newResponse = $res->withHeader('Cache-Control', 'public, max-stale=13910400')->withJson(["message" => "Test"]);
    return $newResponse;
});

CURL 输出

C:\Users\Glenn>curl -X GET -v http://localhost/vms2/public/test

HTTP/1.1 200 OK

Date: Tue, 13 Sep 2016 19:04:42 GMT * Server Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3 is not blacklisted

Server: Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3

X-Powered-By: PHP/5.6.3

Set-Cookie: VMS2=2qf14qr1c0eplgfvibi8t2hcd2; path=/

Expires: Thu, 19 Nov 1981 08:52:00 GMT

Pragma: no-cache

Cache-Control: public, max-stale=13910400

Content-Length: 18

Content-Type: application/json;charset=utf-8

{"message":"Test"}

  • Connection #0 to host localhost left intact

从您的代码中删除缓存控件并在您的 .htaccess 文件中添加以下代码

<filesMatch "\.(html|htm|php)$"> Header set Cache-Control "max-age=1, private, must-revalidate" </filesMatch>