如何在Laravel 5中使用Storage Facade获取文件URL? [英] How to get file URL using Storage facade in laravel 5?
问题描述
我一直在尝试将新的Flysystem与Laravel 5集成在一起.我正在将本地化"路径存储到DB,并让Storage Facade来完成路径.例如,我存储 screenshots/1.jpg
并使用
I've been experimenting using the new Flysystem integration with Laravel 5. I am storing 'localised' paths to the DB, and getting the Storage facade to complete the path. For example I store screenshots/1.jpg
and using
Storage::disk('local')->get('screenshots/1.jpg')
或
Storage::disk('s3')->get('screenshots/1.jpg')
我可以在不同的磁盘上检索相同的文件.
I can retrieve the same file on different disks.
get
检索文件内容,但我希望在这样的视图中使用它:
get
retrieves the file contents, but I am hoping to use it in my views like this:
<img src="{{ Storage::path('screenshots/1.jpg') }}" />
但是路径,或者任何能够检索完整路径的东西都不可用(据我所知).那么如何返回完整路径?或者,我想知道这是否是设计使然?如果是这样,为什么我不应该获得完整的路径?还是我完全以错误的方式来做这件事?
but path, or anything able to retrieve the full path is not available (as far as I can see). So how can I return the full path? Or, I'm wondering if this is by design? If so, why am I not supposed to be able to get the full path? Or, am I going about this completely the wrong way?
推荐答案
适用于L5.2 +的解决方案
有一个更好,更直接的解决方案.
There's a better and more straightforward solution.
使用 Storage :: url($ filename)
获取给定文件的完整路径/URL.请注意,您需要在 config/filesystems.php
中将 S3
设置为存储文件系统:'default'=>'s3'
Use Storage::url($filename)
to get the full path/URL of a given file. Note that you need to set S3
as your storage filesystem in config/filesystems.php
: 'default' => 's3'
当然,您也可以用相同的方法执行 Storage :: disk('s3')-> url($ filename)
.
Of course, you can also do Storage::disk('s3')->url($filename)
in the same way.
正如您在 config/filesystems.php
中看到的那样,还有一个参数'cloud'=>定义的"s3"
,指的是Cloud文件系统.如果要保留本地服务器中的存储文件夹,但要在云中检索/存储一些文件,请使用 Storage :: cloud()
,该文件也具有相同的文件系统方法,即 Storage:: cloud()-> url($ filename)
.
As you can see in config/filesystems.php
there's also a parameter 'cloud' => 's3'
defined, that refers to the Cloud filesystem. In case you want to mantain the storage folder in the local server but retrieve/store some files in the cloud use Storage::cloud()
, which also has the same filesystem methods, i.e. Storage::cloud()->url($filename)
.
Laravel文档没有提及此方法,但是如果您想了解更多有关此方法,可以检查其源代码
The Laravel documentation doesn't mention this method, but if you want to know more about it you can check its source code here.
这篇关于如何在Laravel 5中使用Storage Facade获取文件URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!