Laravel Storage::disk()->url(); 不能正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45132441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Storage::disk()->url(); does not work properly
提问by Kaczor
Hello i have a trouble in laravel.
您好,我在 Laravel 中遇到了麻烦。
i wanna create private storage for some stuff (xls,image,pdf, etc). Everything works great in storage/app/public directories but i dont want it, i want my directory like storage/app/products/{id}/
我想为一些东西(xls、图像、pdf 等)创建私人存储。在 storage/app/public 目录中一切正常,但我不想要它,我想要我的目录,如 storage/app/products/{id}/
at first look at my code:
首先看看我的代码:
filesystem.php
文件系统.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'productions' => [
'driver' => 'local',
'root' => storage_path('app/productions'),
'visibility' => 'private',
],
i create new array 'productions'
我创建了新的数组“生产”
ProductionController.php
生产控制器.php
public function file()
{
return '<img src="'.Storage::disk('productions')->url('7/2.png').'">';
}
web.php (Route)
web.php(路由)
Route::group([
'middleware'=>'roles',
'roles'=>['Administrator','Prefabrykacja','Dyrektor Prefabrykacji']
], function () {
Route::get('/Produkcja',[
'uses'=>'ProductionController@index',
'as'=>'production.index']);
Route::post('/Produkcja/create',[
'uses'=>'ProductionController@create',
'as'=>'production.create']);
Route::get('/Produkcja/file',[
'uses'=>'ProductionController@file',
'as'=>'production.file']);
});
if i return
如果我回来
return '<img src="'.Storage::disk('productions')->url('7/2.png').'">';
Or
或者
return '<img src="'.Storage::disk('local')->url('7/2.png').'">';
result is the same. both line return the image from storage/app/public/7/2.png will display not image storage/app/products/7/2.png
结果是一样的。这两行从 storage/app/public/7/2.png 返回的图像将不显示图像 storage/app/products/7/2.png
how can i display the image from my products folder and restrict resources to specifed role?
如何显示产品文件夹中的图像并将资源限制为指定角色?
Regards
问候
回答by Tajgeer
First of all, you're probably missing a symlink like this one for public
(Local URL Host Customization). Also, you need to specify url
parameter like for public
and change visibility
to public
in order to allow others to see your files.
首先,您可能缺少这样的符号链接public
(本地 URL 主机自定义)。此外,您需要指定url
类似 forpublic
和更改visibility
为的参数public
,以允许其他人查看您的文件。
filesystem.php:
文件系统.php:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'productions' => [
'driver' => 'local',
'root' => storage_path('app/productions'),
'url' => env('APP_URL') . '/productions', // added line (directory within "public")
'visibility' => 'public', // modified visibility
],
],
Also make sure that you create a symlink at public/productions
which will point to storage/app/productions
directory. You can do this by using following command (for example):
还要确保您创建了一个public/productions
指向storage/app/productions
目录的符号链接。您可以使用以下命令执行此操作(例如):
cd LARAVEL_PATH && ln -s storage/app/productions public/productions