gpt4 book ai didi

php - Laravel 响应 Cache-Control header 始终包含 'no-cache'

转载 作者:行者123 更新时间:2023-12-03 00:11:31 25 4
gpt4 key购买 nike

出于某种原因,Laravel 似乎在最后一刻操纵响应 header “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->isCacheable() 也返回 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)时,它不会发送这些 Cache-Control header 。

当我设置last-modified和expires headers时,如何才能让浏览器不接收Cache-Control headers“no-store, no-cache”?

谢谢!

最佳答案

我相信您的幻像缓存控制 header 来自 PHP。

http://php.net/manual/en/function.session-cache-limiter.php

当 php.ini 将 session.cache_limiter 设置为 nocache(默认)时,PHP 设置以下 header :

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 中设置 header 只是将它们附加到 php.ini 设置的 header 中。我尝试在 apache.conf 中设置一些规则,以便允许缓存通过 laravel 访问的 .js 和 .css 文件,同时防止缓存对 .php 文件的请求,但这些规则失败了,因为 apache 会看到任何文件通过 laravel 作为 .php 文件提供(因为它是通过 index.php 访问的)。

最后我决定在 php.ini 中将 session.cache_limiter 设置为 '' (从而跳过 PHP 对缓存 header 的处理),并将以下内容添加到 apps: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");
}

关于php - Laravel 响应 Cache-Control header 始终包含 'no-cache',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32360492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com