mwi/laravel-files

此包已被放弃且不再维护。没有建议的替代包。

MWI 多态文件管理

1.2.0 2021-01-07 15:02 UTC

README

Laravel 的简单直观的多态文件管理服务。

它将支持 Laravel 的任何磁盘驱动器: "本地"、"ftp"、"sftp"、"s3"、"rackspace"。

安装

composer require mwi/laravel-files
php artisan mwi:files:install

别名

如果您想使用门面,请将以下别名添加到您的 config/app.php

'aliases' => [
    // ...
    'MWIFile' => MWI\LaravelFiles\Facades\MWIFile::class,
    // ...
],

服务提供者

如果您使用的是 Laravel 5.5 或更高版本,则服务提供者将自动加载,您可以跳过此步骤。如果不是,请将以下内容添加到您的 config/app.php 提供者

'providers' => [
    // ...
    MWI\LaravelFiles\ServiceProvider::class,
    // ...
],

验证

要验证包是否成功设置,您可以在任何方法中使用 use MWIFile 并调用 MWIFile::verify()

如果成功,它应返回服务的版本。

设置

任何您想集成文件的模型只需添加关系即可

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    // ...

    /**
     * Relationships
     */
    public function files()
    {
        return $this->morphMany(FileUpload::class, 'fileable');
    }

    // Specific type of relationship
    public function photos()
    {
        // Where types value is equal to the value of `fileable_relationship` when saving
        return $this->morphMany(FileUpload::class, 'fileable')->where('type', 'photos');
    }
}

文件上传

您可以使用任何数量的方法来上传您的文件。

注意 对于 CSRF 或 HTTP 方法,以下字段可用于使用

  • file 必需 包含要上传的文件
  • fileable_type 是您保存文件的模型命名空间
  • fileable_id 是要附加到的特定资源的 ID
  • fileable_relationship 引用了您在模型中创建的关系名称

基本表单

最基本的是简单的单次表单字段。您可以根据需要添加任何数量的其他输入。

<form action="{{ route('file-upload') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <input type="file" name="file">
    <input type="hidden" value="\App\User" name="fileable_type">
    <input type="hidden" value="{{ $user->id }}" name="fileable_id">
    <input type="hidden" value="files" name="fileable_relationship">
</form>

用法

一旦您安装了该包并设置了视图,就有三种可用方法可供使用

/**
 * @param  \Illuminate\Http\UploadedFile  $file  The uploaded file
 *
 * @param  string                         $disk  The disk in which to upload the file too,
 *                                               defaults to local, meaning it will not be publicly accessible.
 *                                               Change to `public` for public files like profile photos.
 *                                               It's recommend to use `config('filesystems.default')` as a standard
 *                                               and then chagne as necessary for specific use cases
 *
 * @param  Array                          $data  An array requiring at least the following data, note that
 *                                               if this data is not all present it will simply upload the file
 *                                               and not be associtaed to a specific model:
 *                                               fileable_type
 *                                               fileable_id
 *                                               fileable_relationship
 */
MWIFile::upload($request->file('file'), 'local', $request->input());

/**
 * @param \App\FileUpload $file The file resource
 *
 * Note that if the file is publicly accessiblethis
 * will redirect to it rather than initialize a download
 */
MWIFile::download(FileUpload::latest()->first());

/**
 * @param \App\FileUpload $file The file resource
 *
 * This just removed the relationship to the file,
 * it does NOT delete the file from the filesystem
 */
MWIFile::remove(FileUpload::latest()->first());