我在 laravel 中找不到 Storage Facade 的实现

I can't find the implementation for Storage Facade in laravel

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

我正在使用 laravel "Storage" 门面 但是当我输入

我在我的代码中引用了上面的 class

 use Illuminate\Support\Facades\Storage;

例如:

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

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

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';
    }
}

所以实现在哪里,如果你有其他方法来处理 文件系统进程而不是 "Storage" 外观 ??

存储是 facade 并访问位于此处的 class Filesystemvendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php

如您在 official filesystem documentation 中所见,代码片段使用 Storage

更新:

您应该添加 use Storage; 才能使用 Storage 门面。

public_path 函数 returns public 目录的完全限定路径,即 laravel 应用程序内的 public 目录。使用存储时,路径设置为 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);
}

我建议阅读 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());
}

如果您的文件上传可以是单个 and/or 多个,我建议使用数组方法,因为包裹在数组中的单个文件允许您对单个和多个上传使用相同的语法(即: 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 的重要部分是使用 <img src="/storage/examples/slug/filename.jpg"> 如果您的文件位于您的存储库中 /storage/app/public/examples/slug/filename.jpg 请密切注意每个字符。