Rie*_*art 12 php header response no-cache laravel
出于某种原因,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;
}
}
Run Code Online (Sandbox Code Playgroud)
即使我使用"中间件之后"并且死掉了转发响应,我仍然会得到这个,这似乎对我来说是正确的.
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
}
Run Code Online (Sandbox Code Playgroud)
方法$ response-> isCacheable()als返回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
Run Code Online (Sandbox Code Playgroud)
我使用xampp,但在我加载一个html页面(没有Laravel/PHP)时在同一台服务器上,它不会发送这些Cache-Control标头.
当我设置最后修改和过期的标头时,如何实现浏览器不会收到Cache-Control标头"no-store,no-cache"?
谢谢!
小智 6
我相信您的幻影缓存控制标头来自PHP。
http://php.net/manual/zh/function.session-cache-limiter.php
当php.ini将session.cache_limiter设置为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
Run Code Online (Sandbox Code Playgroud)
我已经在apache上的laravel中进行高速缓存控制了几天了:我发现在laravel中设置标头只是将它们附加到php.ini设置的标头上。我尝试在apache.conf中设置一些规则,以允许对通过laravel访问的.js和.css文件进行缓存,同时阻止对.php文件的请求进行缓存,但是这些规则失败了,因为apache会看到任何文件通过laravel作为.php文件提供服务(因为正在通过index.php访问该文件)。
最后,我决定在php.ini中将session.cache_limiter设置为”(从而跳过PHP对缓存头的处理),然后将以下内容添加到app:after()中的filters.php中
/*
* 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");
}
Run Code Online (Sandbox Code Playgroud)
虽然我不知道您的确切配置,但我认为这是由于您的 Apache 配置造成的,因为标头值可以在那里被覆盖。
查看所有 Apache 配置文件并查找以 开头的行Header Set Cache-Control,例如Header Set Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
可能这样的指令设置为仅影响您的 PHP 文件,这就是为什么其他文件与其他标头一起传递的原因。
但是:更改此设置时要小心。也许出于安全原因您希望进行此设置。考虑通过代理缓存动态、经过身份验证的内容的问题(详细信息链接)