简体   繁体   English

无法在Homestead上的Laravel 5.2中发送存储文件夹的文件响应

[英]Can’t send a file response for the storage folder in Laravel 5.2 on Homestead

What I wanted to do, is to protect my private files, so that only their owners could access them. 我要做的是保护我的私人文件,以便只有其所有者才能访问它们。 I changed my public disk to storage folder: 我将公用磁盘更改为存储文件夹:

'orders' => [
    'driver' => 'local',
    'root'   => storage_path('files/orders'),
],

And created a routed controller to be able to make files private (I intentionally excluded this logic from the example): 并创建了一个路由控制器,以便能够将文件设为私有(我故意从示例中排除了此逻辑):

Route::get('files/{slug}', [
    'as'   => 'get.file',
    'uses' => 'FileController@getFile',
]);

class FileController extends Controller
{
    public function getFile($slug)
    {
        // I use spatie/laravel-medialibrary
        // to manage file uploads.
        $media = Media::where('slug', $slug)->first();

        return response()->file($media->getPath());
    }
}

The getPath() is a spatie/laravel-medialibrary method that returns the correct path to the file: /home/vagrant/code/project/storage/files/orders/2/b414a7416571145ea9dcf9bda9a845a2.jpg . getPath()spatie/laravel-medialibrary方法,它返回文件的正确路径: /home/vagrant/code/project/storage/files/orders/2/b414a7416571145ea9dcf9bda9a845a2.jpg It clearly finds the file, because I can use \\Symfony\\Component\\HttpFoundation\\File\\File() to get files' data like their mime type or size (and also don't get a 404 error as a response); 它可以清楚地找到文件,因为我可以使用\\Symfony\\Component\\HttpFoundation\\File\\File()来获取文件的数据,例如它们的mime类型或大小(并且也不会收到404错误)。 when I follow, say, http://localhost:3000/files/2RmYR3 route I get this blank 200 response—looks like so: 当我遵循例如http://localhost:3000/files/2RmYR3路由时,我得到此空白200响应-看起来像这样:

在此处输入图片说明

One person tested pretty much the same code on his end, and it worked, but he was on Valet, so it might be a Homestead issue. 一个人在他的末端测试了几乎相同的代码,并且该代码有效,但是他在Valet上,所以这可能是Homestead问题。 He also suggested it might be an issue with headers, and I tried sending different headers (content-type/length, for instance), but nothing helped, so please see what I'm receiving without specifying any headers for the response: 他还暗示,标题可能是一个问题,我尝试发送不同的标题(例如,内容类型/长度),但没有任何帮助,因此请查看我收到的内容而未为响应指定任何标题:

在此处输入图片说明

I also tried vagrant destroy , just in case, but the issue stayed. 我还尝试了vagrant destroy ,以防万一,但是问题仍然存在。 In any case, thank you for your time. 无论如何,谢谢您的时间。

It resolved itself. 它解决了自己。 I started stripping pieces from the application to understand which part of the code messes with responses, at first I thought it was my view composer, but no; 我开始从应用程序中剥离代码,以了解代码的哪一部分与响应混淆了,起初我以为是我的视图编辑器,但是没有。 I did a fresh separate Laravel installation, and copied config/app.php from the fresh installation to my project, then just updated lists of providers and aliases that I had before, and that did it. 我做了一个单独的全新Laravel安装,并将config/app.php从全新安装复制到了我的项目,然后只是更新了以前拥有的提供程序和别名的列表,然后执行了。

My app.php lacked 'log_level' => env('APP_LOG_LEVEL', 'debug'), line that was added at some point to Laravel after I started the project, and maybe that's why responses didn't work properly, but I doubt it. 我的app.php缺少'log_level' => env('APP_LOG_LEVEL', 'debug'),这是在我启动项目后的某个时候在Laravel上添加的行,也许这就是为什么响应无法正常工作的原因,但是我怀疑它。

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

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