简体   繁体   English

无法在Laravel 4中设置Cache-Control

[英]Cannot set Cache-Control in Laravel 4

I have written a controller in Laravel 4 to serve up static resources such as image files with some application-level tracking. 我在Laravel 4中编写了一个控制器来提供静态资源,例如带有一些应用程序级跟踪的图像文件。 I don't want people having to constantly hit the web server for each image, though, so I'm trying to set a Cache-Control header. 我不希望人们不得不经常为每个图像点击Web服务器,所以我试图设置一个Cache-Control头。 I'm at my wit's end here, it's just plain not working, and I cannot figure out why. 我在这里结束了我的智慧,它显然不起作用,我无法弄清楚原因。 Can someone please take a look at the following and tell me what I'm doing wrong? 有人可以看看以下内容并告诉我我做错了什么吗?

$headers = array(
    'Content-Type' => 'image/png',
    'Cache-Control' => 'public, max-age=300',
    'Content-Length' => filesize($file),
    'Expires' => date('D, d M Y H:i:s ', time() + 300).'GMT',
);
return Response::stream(function() use ($file) {
    readfile($file); }, 200, $headers);

When I access the file, it works almost perfectly. 当我访问该文件时,它几乎完美地工作。 The image is displayed, and when I go in and inspect the element using Chrome or Firefox, I can see that the Expires header is set correctly (when I change it and force a hard reload, it resets it appropriately). 显示图像,当我进入并使用Chrome或Firefox检查元素时,我可以看到Expires标头设置正确(当我更改它并强制重新加载时,它会适当地重置它)。 But no matter what I try, the Cache-Control header is always set to "no-cache, private". 但无论我尝试什么,Cache-Control标头总是设置为“no-cache,private”。 I've tried using the following, all to no avail: 我尝试过使用以下内容,但都无济于事:

$headers['Cache-Control'] = 'public, max-age=300'; // Original setting
$headers['cache-control'] = 'public, max-age=300'; // Case-sensitive?
$headers['Cache-Control'] = 'max-age=300';
$headers['Cache-Control'] = 'private, max-age=300';
$headers['Cache-Control'] = 'max-age=300, public';
$headers['Cache-Control'] = 'max-age=300, private';

But like I said, no matter what I set it to, it always returns a response header showing Cache-Control: no-cache, private. 但就像我说的那样,无论我将它设置为什么,它总是会返回一个响应头,显示Cache-Control:no-cache,private。

What am I doing wrong? 我究竟做错了什么? How can I get my image and binary files returned via this controller to cache? 如何通过此控制器返回我的图像和二进制文件进行缓存? Has Laravel 4 for some reason hard-coded the Cache-Control header in all responses? 由于某些原因,Laravel 4是否在所有响应中对Cache-Control标头进行了硬编码?

See this issue: https://github.com/symfony/symfony/issues/6530 and this line: https://github.com/symfony/symfony/blob/2.4/src/Symfony/Component/HttpFoundation/StreamedResponse.php#L87 看到这个问题: https//github.com/symfony/symfony/issues/6530和这一行: https//github.com/symfony/symfony/blob/2.4/src/Symfony/Component/HttpFoundation/StreamedResponse.php #L87

It suggests to use BinaryFileResponse instead. 它建议使用BinaryFileResponse。 Something like this: 像这样的东西:

$response = new Symfony\Component\HttpFoundation\BinaryFileResponse($file);
$response->setContentDisposition('inline');
$response->setTtl(300);
return $response;

//Or with setting the headers manually
return  new Symfony\Component\HttpFoundation\BinaryFileResponse($file, 200, $headers, true, 'inline');

Now you can set the Cache-Control headers. 现在您可以设置Cache-Control标头。

Or using the Response::download() facade: 或使用Response :: download()外观:

return Response::download($file)->setTtl(300)->setContentDisposition('inline');
return Response::download($file, null, $headers)->setContentDisposition('inline');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM