For some reason Laravel seems to be manipulating the response headers 'Cache-Control' on the very last moment. I want to make browser caching possible.
出于某种原因,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;
}
}
Even when I use a 'after-middleware' and die and dump the response, I still get this, what seems to be right to me.
即使当我使用“后中间件”并终止并转储响应时,我仍然会得到这个,对我来说似乎是正确的。
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
}
The method $response->isCacheable() als returns true. But when I receive the response, Firebug shows the following:
方法$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
I use xampp, but on this same server when I just load an html-page (no Laravel/PHP), it does not send these Cache-Control headers.
我使用xampp,但是当我只加载html页面(没有Laravel/PHP)时,它不会发送这些Cache-Control头。
How can I achieve that the browser does not receive the Cache-Control headers "no-store, no-cache" when I set the last-modified and expires headers?
当我设置最后修改的和过期的页眉时,我如何实现浏览器不接收“无存储、无缓存”的缓存控制页眉?
Thanks!
谢谢!
2 个解决方案
#1
1
I believe your phantom cache-control headers are coming from PHP.
我相信您的幻影缓存控制头来自PHP。
http://php.net/manual/en/function.session-cache-limiter.php
http://php.net/manual/en/function.session-cache-limiter.php
when php.ini has session.cache_limiter set to nocache (default), PHP sets the following headers:
当php。ini会话。将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
I have been struggling with cache-control in laravel on apache for a few days now: I found that setting the headers within laravel simply appended them on to the headers set by php.ini. I tried setting up some rules in apache.conf in order to allow caching of .js and .css files that were being accessed via laravel while preventing the caching of requests to .php files, but these rules failed as apache will see any file being served via laravel as a .php file (because it is being accessed via index.php).
最近几天,我一直在apache上的laravel的cache-control上苦苦挣扎:我发现在laravel中设置header仅仅是将它们附加到php.ini设置的header上。我尝试在apache中建立一些规则。参看为了让缓存的. js和css文件被访问通过laravel而防止缓存的请求。php文件,但这些规则没有apache将看到任何文件是通过laravel。php文件被访问(因为它是通过index . php)。
In the end I settled for setting session.cache_limiter to '' in php.ini (thereby skipping PHPs handling of cache headers), and adding the following to filters.php in app:after()
最后我决定设置会话。cache_limiter设为“php”。ini(因此跳过对缓存头的PHPs处理),并向过滤器添加以下内容。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");
}
#2
0
Although I do not know your exact configuration, I would assume that this is due to your Apache configuration, as header values can be overwritten there.
尽管我不知道您的确切配置,但我假设这是由于您的Apache配置,因为可以在那里覆盖头值。
Have a look through all Apache configuration files and look out for lines starting with Header Set Cache-Control
, e.g. Header Set Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
查看所有Apache配置文件并查找以头集缓存控制开始的行,例如头集缓存控制“no-store, no-cache, must-revalidate, post-check=0, pre-check=0”
Probably such a directive is set to affect only your PHP files, which would be the reason why other files are delivered with other headers.
可能这样的指令只会影响PHP文件,这就是为什么其他文件会与其他头文件一起交付的原因。
However: Watch out when changing this. Maybe you would want this to be set for security reasons. Consider the problems with caching dynamic, authenticated content by proxies (link for detail)
然而,当改变这一点时要小心。也许你会希望出于安全原因设置这个。考虑使用代理缓存动态的、经过身份验证的内容的问题(详细链接)
#1
1
I believe your phantom cache-control headers are coming from PHP.
我相信您的幻影缓存控制头来自PHP。
http://php.net/manual/en/function.session-cache-limiter.php
http://php.net/manual/en/function.session-cache-limiter.php
when php.ini has session.cache_limiter set to nocache (default), PHP sets the following headers:
当php。ini会话。将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
I have been struggling with cache-control in laravel on apache for a few days now: I found that setting the headers within laravel simply appended them on to the headers set by php.ini. I tried setting up some rules in apache.conf in order to allow caching of .js and .css files that were being accessed via laravel while preventing the caching of requests to .php files, but these rules failed as apache will see any file being served via laravel as a .php file (because it is being accessed via index.php).
最近几天,我一直在apache上的laravel的cache-control上苦苦挣扎:我发现在laravel中设置header仅仅是将它们附加到php.ini设置的header上。我尝试在apache中建立一些规则。参看为了让缓存的. js和css文件被访问通过laravel而防止缓存的请求。php文件,但这些规则没有apache将看到任何文件是通过laravel。php文件被访问(因为它是通过index . php)。
In the end I settled for setting session.cache_limiter to '' in php.ini (thereby skipping PHPs handling of cache headers), and adding the following to filters.php in app:after()
最后我决定设置会话。cache_limiter设为“php”。ini(因此跳过对缓存头的PHPs处理),并向过滤器添加以下内容。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");
}
#2
0
Although I do not know your exact configuration, I would assume that this is due to your Apache configuration, as header values can be overwritten there.
尽管我不知道您的确切配置,但我假设这是由于您的Apache配置,因为可以在那里覆盖头值。
Have a look through all Apache configuration files and look out for lines starting with Header Set Cache-Control
, e.g. Header Set Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
查看所有Apache配置文件并查找以头集缓存控制开始的行,例如头集缓存控制“no-store, no-cache, must-revalidate, post-check=0, pre-check=0”
Probably such a directive is set to affect only your PHP files, which would be the reason why other files are delivered with other headers.
可能这样的指令只会影响PHP文件,这就是为什么其他文件会与其他头文件一起交付的原因。
However: Watch out when changing this. Maybe you would want this to be set for security reasons. Consider the problems with caching dynamic, authenticated content by proxies (link for detail)
然而,当改变这一点时要小心。也许你会希望出于安全原因设置这个。考虑使用代理缓存动态的、经过身份验证的内容的问题(详细链接)