Laravel 5 - 如何在 View 中访问存储中上传的图像? [英] Laravel 5 - How to access image uploaded in storage within View?

查看:22
本文介绍了Laravel 5 - 如何在 View 中访问存储中上传的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Laravel 存储中上传了用户的头像.如何访问它们并在视图中呈现它们?

服务器将所有请求都指向 /public,如果它们在 /storage 文件夹中,我如何显示它们?

解决方案

最佳方法是创建一个符号链接,就像@SlateEntropy在下面的答案.为了解决这个问题,从 5.3 版开始,Laravel 包含一个命令,这使得这非常容易做:

php artisan storage:link

这会为您创建从 public/storagestorage/app/public 的符号链接,这就是它的全部内容.现在 /storage/app/public 中的任何文件都可以通过如下链接访问:

http://somedomain.com/storage/image.jpg

<小时>

如果您出于任何原因无法创建符号链接(可能您使用的是共享主机等),或者您想保护某些访问控制逻辑背后的某些文件,则可以选择使用特殊路由读取并提供图像.例如一个简单的关闭路线,如下所示:

Route::get('storage/{filename}', function ($filename){$path = storage_path('public/' . $filename);如果 (!File::exists($path)) {中止(404);}$file = File::get($path);$type = File::mimeType($path);$response = Response::make($file, 200);$response->header("Content-Type", $type);返回 $response;});

您现在可以像使用符号链接一样访问您的文件:

http://somedomain.com/storage/image.jpg

如果您使用的是 Intervention Image Library,您可以使用其内置的 响应方法使事情更简洁:

Route::get('storage/{filename}', function ($filename){return Image::make(storage_path('public/' . $filename))->response();});

<小时><块引用>

警告

请记住,手动提供文件会导致性能损失,因为您要经历整个 Laravel 请求生命周期才能读取和发送文件内容,这比让 HTTP 服务器处理它慢得多.

I have got user's avatars uploaded in Laravel storage. How can I access them and render them in a view?

The server is pointing all requests to /public, so how can I show them if they are in the /storage folder?

解决方案

The best approach is to create a symbolic link like @SlateEntropy very well pointed out in the answer below. To help with this, since version 5.3, Laravel includes a command which makes this incredibly easy to do:

php artisan storage:link

That creates a symlink from public/storage to storage/app/public for you and that's all there is to it. Now any file in /storage/app/public can be accessed via a link like:

http://somedomain.com/storage/image.jpg


If, for any reason, your can't create symbolic links (maybe you're on shared hosting, etc.) or you want to protect some files behind some access control logic, there is the alternative of having a special route that reads and serves the image. For example a simple closure route like this:

Route::get('storage/{filename}', function ($filename)
{
    $path = storage_path('public/' . $filename);

    if (!File::exists($path)) {
        abort(404);
    }

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});

You can now access your files just as you would if you had a symlink:

http://somedomain.com/storage/image.jpg

If you're using the Intervention Image Library you can use its built in response method to make things more succinct:

Route::get('storage/{filename}', function ($filename)
{
    return Image::make(storage_path('public/' . $filename))->response();
});


WARNING

Keep in mind that by manually serving the files you're incurring a performance penalty, because you're going through the entire Laravel request lifecycle in order to read and send the file contents, which is considerably slower than having the HTTP server handle it.

这篇关于Laravel 5 - 如何在 View 中访问存储中上传的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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