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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:18:22  来源:igfitidea点击:

Laravel Storage::disk()->url(); does not work properly

phplaravellaravel-5.4

提问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 urlparameter like for publicand change visibilityto publicin 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/productionswhich will point to storage/app/productionsdirectory. 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