【问题标题】:I can't find the implementation for Storage Facade in laravel我在 laravel 中找不到 Storage Facade 的实现
【发布时间】:2016-05-13 12:08:01
【问题描述】:

我是 laravel 新手,我在 laravel 上的 fileststem 工作 (我想做通常的fileststem过程,比如-make dir - copy - put -delete -ect)

我正在使用 laravel“存储”外观 但是当我输入

我在我的代码中像这样引用了上面的类

 use Illuminate\Support\Facades\Storage;

例如下面:

 if (file_exists(public_path($oldImage))) {
                Storage::delete($oldImage);
            }

什么也没发生,当我参考类代码时,我发现了这个:

namespace Illuminate\Support\Facades;

/**
 * @see \Illuminate\Filesystem\FilesystemManager
 */
class Storage extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'filesystem';
    }
}

那么实施在哪里,如果您有其他方法来处理 文件系统进程而不是“存储”门面??

【问题讨论】:

    标签: laravel filesystems storage


    【解决方案1】:

    Storage 是 facade 并访问位于此处的类 Filesystemvendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php

    正如您在 official filesystem documentation 中看到的,代码 sn-ps 使用 Storage

    更新:

    您应该添加use Storage; 才能使用Storage 外观。

    【讨论】:

    • 我在构造函数中使用了“文件系统”并且它可以工作,但回到我最初的问题,为什么“存储”门面类在我的情况下不起作用,为什么它没有指向“文件系统”类!!
    • 很好用!你刚才问Storage 的实现在哪里,它在我帖子的文件中;)
    【解决方案2】:

    我建议阅读 Laravel 8.X 文档以初步了解:https://laravel.com/docs/8.x/filesystem

    注意:在你过于得意忘形之前,请确保你了解localpublic 之间的区别。

    对于初学者,您的首要目标应该是上传文件并获取UploadedFile 类型。

    您可以通过$request->file('name') 之类的方式访问单个文件,或者通过以下方式访问一组图像:

    // $request->input('images')
    
    foreach ($images as $image) {
        \Log::debug($image->getClientOriginalName());
    }
    

    如果您的文件上传可以是单个和/或多个,我建议使用数组方法,因为包含在数组中的单个文件允许您对单个和多个上传使用相同的语法(即:foreach 循环可以正常工作一张图片,没有额外的代码)。

    这是一个例子:

    use Illuminate\Support\Facades\Storage;
    
    $slug = 'davids-sandwich-photos';
    
    foreach ($images as $image) {
        Storage::putFileAs(
            'images' .'/'. $slug,
            $image,
            $image->getClientOriginalName()
        );
    }
    

    Storage::putFileAs() 可以带 3 个参数:目录、内容、文件名。您可以在上面的代码中看到我插入了静态和派生目录名称的混合。您可以执行'images' .'/'. $slug .'/'. Auth::user()->id 之类的操作将文件保存在/images/davids-sandwich-photos/11 中。

    然后,检查您的 repo 目录:/storage/app/ 并查找 images 目录。

    您可以在测试时手动删除这些文件夹,以使您的方向正确。

    这应该足以让大多数人开始。

    为了避免使用Storage门面,你可以使用:

     foreach ($images as $image) {
         $image->storeAs(
             'examples' .'/'. $slug,
             $image->getClientOriginalName(),
             'public'
         );
     }
    

    --

    如果您想开始操作驱动程序,请查看disks 部分下的config/filesystems.php,但我不是这里的数据库管理员专家。

    我还在旅途中保存了这个:https://medium.com/@shafiya.ariff23/how-to-store-uploaded-images-in-public-folder-in-laravel-5-8-and-display-them-on-shared-hosting-e31c7f37a737。如果您遇到符号链接之类的问题,您可能需要它。

                <img
                    v-for="image in example.images"
                    :key="image.filename"
                    :src="`/storage/examples/${example.slug}/${image.filename}`"
                >
    

    注意:Vue JS 的重要部分是使用 &lt;img src="/storage/examples/slug/filename.jpg"&gt; 如果您的文件位于存储库中,则为 /storage/app/public/examples/slug/filename.jpg 密切注意每个字符。

    【讨论】:

      【解决方案3】:

      public_path 函数返回公共目录的完全限定路径,即 laravel 应用程序内部的公共目录。使用Storage时,路径设置为storage/app目录。

      if (file_exists(public_path($oldImage))) {
        //public_path($oldImage) will check for file in public directory
        Storage::delete($oldImage); //Will delete file in storage/app directory
      }
      

      修改后的代码应该是

      if(Storage::has($oldImage)){
          Storage::delete($oldImage);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-24
        • 2017-11-03
        • 2019-04-21
        • 2016-09-30
        • 2017-08-29
        • 2019-04-05
        • 2020-10-24
        • 2020-12-28
        相关资源
        最近更新 更多