无法在Laravel 4中设置Cache-Control [英] Cannot set Cache-Control in Laravel 4

查看:274
本文介绍了无法在Laravel 4中设置Cache-Control的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 4中编写了一个控制器来提供静态资源,例如带有一些应用程序级跟踪的图像文件。我不希望人们不得不经常为每个图像点击Web服务器,所以我正在尝试设置Cache-Control标头。我在这里结束了我的智慧,它显然不起作用,我无法弄清楚原因。有人可以看看以下内容并告诉我我做错了什么吗?

I have written a controller in Laravel 4 to serve up static resources such as image files with some application-level tracking. 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. 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);

当我访问该文件时,它几乎完美无缺。显示图像,当我进入并使用Chrome或Firefox检查元素时,我可以看到Expires标头设置正确(当我更改它并强制重新加载时,它会适当地重置它)。但无论我尝试什么,Cache-Control标头总是设置为no-cache,private。我尝试过使用以下内容,但都无济于事:

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). But no matter what I try, the Cache-Control header is always set to "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';

但就像我说的那样,无论我把它设置为什么,它总是返回显示Cache-Control的响应标头:no-cache,private。

But like I said, no matter what I set it to, it always returns a response header showing Cache-Control: no-cache, private.

我做错了什么?如何通过此控制器返回我的图像和二进制文件进行缓存?由于某些原因,Laravel 4是否在所有响应中对Cache-Control标头进行了硬编码?

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?

推荐答案

请参阅此问题: https://github.com/symfony/symfony/issues/6530 此行: https://github.com/symfony/symfony/ blob / 2.4 / src / Symfony / Component / HttpFoundation / StreamedResponse.php#L87

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

它建议使用BinaryFileResponse。这样的事情:

It suggests to use BinaryFileResponse instead. 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');

现在您可以设置Cache-Control标头。

Now you can set the Cache-Control headers.

或使用Response :: download()facade:

Or using the Response::download() facade:

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

这篇关于无法在Laravel 4中设置Cache-Control的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆