由于某种原因,Laravel似乎在最后一刻操纵响应头'Cache-Control'。我想让浏览器缓存成为可能。
class TestController extends Controller
{
public function getTest()
{
$response = new 'Illuminate'Http'Response('test', 200, array(
'Cache-Control' => 'max-age='.(config('imagecache.lifetime')*60).', public',
'Content-Length' => strlen('test'),
));
$response->setLastModified(new 'DateTime('now'));
$response->setExpires('Carbon'Carbon::now()->addMinutes(config('imagecache.lifetime')));
return $response;
}
}
即使当我使用'后中间件'并且死亡并转储响应时,我仍然得到这个,这对我来说似乎是正确的。
Response {#625 ▼
+original: "test"
+exception: null
+headers: ResponseHeaderBag {#626 ▼
#computedCacheControl: array:2 [▼
"max-age" => "2592000"
"public" => true
]
#cookies: []
#headerNames: array:5 [▶]
#headers: array:5 [▼
"cache-control" => array:1 [▼
0 => "max-age=2592000, public"
]
"content-length" => array:1 [▼
0 => 4
]
"date" => array:1 [▶]
"last-modified" => array:1 [▼
0 => "Sun, 16 Aug 2015 15:42:08 GMT"
]
"expires" => array:1 [▶]
]
#cacheControl: array:2 [▼
"max-age" => "2592000"
"public" => true
]
}
#content: "test"
#version: "1.0"
#statusCode: 200
#statusText: "OK"
#charset: null
}
方法$response-> isacheable()也返回true。但是当我收到响应时,Firebug显示以下内容:
Cache-Control
no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection
Keep-Alive
Content-Type
text/html
Date
Sun, 16 Aug 2015 15:42:08 GMT
Expires
Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive
timeout=5, max=98
Pragma
no-cache
Server
Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15
Transfer-Encoding
chunked
X-Powered-By
PHP/5.5.15
我使用xampp,但在同一台服务器上,当我只是加载一个html页面(没有Laravel/PHP),它不发送这些缓存控制头。
我怎样才能实现浏览器不接收缓存控制头"无存储,无缓存"当我设置最后修改和过期头?
谢谢!
我相信你的虚幻缓存控制头来自PHP。
http://php.net/manual/en/function.session-cache-limiter.php 当php.ini有会话时。cache_limititer
设置为nocache(默认),PHP设置以下头:Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store,no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
我已经在apache上的laravel的缓存控制中挣扎了几天了:我发现在laravel中设置头文件只是将它们附加到php.ini设置的头文件上。我尝试在apache.conf中设置一些规则,以便允许缓存通过laravel访问的。js和。css文件,同时防止缓存请求到。php文件,但这些规则失败了,因为apache会看到任何文件通过laravel作为。php文件服务(因为它是通过index.php访问的)。
最后我决定设置会话。php.ini中的cache_limititer为"(从而跳过php对缓存头的处理),并在app中的filters.php中添加以下内容:after()
/*
* Custom cache headers for js and css files
*/
if ($request->is('*.js') || $request->is('*.css')){
$response->header("pragma", "private");
$response->header("Cache-Control", " private, max-age=86400");
} else {
$response->header("pragma", "no-cache");
$response->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0");
}
虽然我不知道您的确切配置,但我认为这是由于您的Apache配置,因为头值可以在那里被覆盖。
查看所有Apache配置文件,查找以Header Set Cache-Control
开头的行,例如Header Set Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
可能这样的指令被设置为只影响你的PHP文件,这就是为什么其他文件与其他头一起传递的原因。
然而:更改时要小心。也许出于安全考虑,您希望设置这个值。考虑通过代理缓存动态的、经过身份验证的内容的问题(详细信息请链接)