我必须返回一个特定的缓存控制标头(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;
如何正确设置标题?
谢谢
答案 0 :(得分:2)
我怀疑你可能有中间件问题。
上面的代码确实产生了正确的输出。
$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&gt; 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;路径= /
Expires:Thu,1981年11月19日08:52:00 GMT
Pragma:no-cache
Cache-Control:public,max-stale = 13910400
内容长度:18
Content-Type:application / json; charset = utf-8
{ “消息”: “测试”}
- 连接#0以托管localhost保持原样
答案 1 :(得分:0)
从代码中删除缓存控件,并将以下代码添加到您的.htaccess文件中
<filesMatch "\\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>