我必须返回一个特定的缓存控制头(Cache-Control: public, max-stale=13910400
),但在运行时,我得到这个:
缓存控制已被复制,但我只需要自定义值。
$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;
如何正确设置标题?
谢谢
我怀疑你可能有中间件问题。
上面的代码确实产生了正确的输出。
$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好的
日期:2016年9月13日星期二19:04:42 GMT *服务器Apache / 2.4.10(Win32)OpenSSL / 1.0.1i PHP / 5.6.3未列入黑名单
服务器:Apache / 2.4.10(Win32)OpenSSL / 1.0.1i PHP / 5.6.3
X-Powered-By:PHP / 5.6.3
Set-Cookie:VMS2 = 2qf14qr1c0eplgfvibi8t2hcd2;路径= /
到期日:1981年11月19日星期四08:52:00 GMT
Pragma:没有缓存
Cache-Control:public,max-stale = 13910400
内容长度:18
Content-Type:application / json; charset = utf-8
{ “消息”: “测试”}
- 连接#0到主机localhost保持不变
从代码中删除缓存控件,并在.htaccess文件中添加以下代码
<filesMatch "\\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>