solbeg / laravel-files-manager
为Laravel应用程序扩展文件逻辑。简单的文件上传。在模型中使用文件,类似于其他任何简单属性。
Requires
- php: >=5.5.9
- illuminate/console: 5.2.*|5.3.*
- illuminate/container: 5.2.*|5.3.*
- illuminate/contracts: 5.2.*|5.3.*
- illuminate/database: 5.2.*|5.3.*
- illuminate/events: 5.2.*|5.3.*
- illuminate/filesystem: 5.2.*|5.3.*
- illuminate/http: 5.2.*|5.3.*
- illuminate/session: 5.2.*|5.3.*
- illuminate/support: 5.2.*|5.3.*
- illuminate/validation: 5.2.*|5.3.*
- paragonie/random_compat: >=1.0
- symfony/http-foundation: 2.8.*|3.0.*|3.1.*
Suggests
- intervention/image: Allows to use image formatters. Suggested version: 2.3.*.
This package is not auto-updated.
Last update: 2024-09-23 13:52:53 UTC
README
laravel 5.2/5.3的文件管理器。有助于简单地上传并使用未来文件,并像处理其他简单属性一样处理它们。
它很灵活,您可以自定义验证、文件的格式化版本(例如缩略图)、默认URL、存储方式等。您可以根据上下文分离文件,并根据类型分离上下文。您还可以配置全局设置,该设置将用于所有上下文。
此插件还允许在请求之间在临时目录中保存文件。因此,如果请求数据的验证失败,用户不必再次上传文件。
Solbeg/laravel-files-manager使用标准的laravel的文件系统来存储文件,因此您可以使用它与任何存储驱动程序(如本地、Amazon S3、Rackspace或其他)一起使用。
对于图像处理,此包使用Intervention/image插件。
要求
- PHP >= 5.5.9
- Fileinfo 扩展
- Laravel 框架 5.2.* 或 5.3.*
- Intervention/image插件(仅用于处理图像)
安装
安装此插件的最佳方式是使用Composer。
要安装最新版本,运行以下命令
$ php composer.phar require solbeg/laravel-files-manager
安装此供应商后,添加以下行。
在您的Laravel配置文件 config/app.php
中的 $providers
数组中添加此包的服务提供者。
// ... Solbeg\FilesManager\FilesManagerServiceProvider::class, // ...
在您的应用程序 Kernel.php
类中,在 web
中间件组中添加此包的中间件。
// ... \Solbeg\FilesManager\StoreUploadedFilesMiddleware::class, // ...
如果您将使用标准的laravel公共路径,则需要从 public/storage
创建到 storage/app/public
目录的符号链接。您可以在此处了解相关信息这里。
如果您尚未创建它,您可以通过Artisan命令快速完成
$ php artisan filecontext:create-symlink
intervention/image的安装
如果您想处理图像,您可能想要安装intervention/image
插件。因为此文件管理器中所有与图像工作的格式化器都要求使用它。
要安装intervention/image
插件,请按照以下说明操作或阅读官方文档(Composer在Laravel中的安装和集成):这里。
通过Composer安装
$ php composer.phar require intervention/image
安装完Intervention Image后,打开您的Laravel配置文件 config/app.php
并添加以下行。
在 $providers
数组中添加此包的服务提供者。
// ... Intervention\Image\ImageServiceProvider::class, // ...
将此包的别名添加到 $aliases
数组中。
// ... 'Image' => Intervention\Image\Facades\Image::class, // ...
配置
首先,我们建议发布config/filemanager.php
配置文件,这样您可以快速配置插件。
您可以使用Artisan控制台命令完成此操作。
$ php ./artisan vendor:publish --provider="Solbeg\FilesManager\FilesManagerServiceProvider" --tag=config
此插件中的所有文件都根据上下文分开。例如:您可能想要有'product-logo'和'user-avatar'上下文,因此每个上下文都有自己的自定义配置(验证、格式化器等)。
因此,你可能想要创建第一个上下文。你可以有两种方式:在你的配置文件夹中添加配置或者在模型中直接定义。我们推荐第一种方式,因为它可以使你的模型更整洁。
为了快速创建上下文,你可以使用 artisan 控制台命令。
$ php ./artisan make:filecontext {context-name}
其中 context-name
应替换为你新上下文的名称,例如 product-logo
。
然后查看文件 config/filecontexts/{context-name}.php
以获取更多关于可用设置的详细信息。
配置模型
你可能想要处理文件,其名称存储在数据库中。因此,你应该配置你的 Eloquent 模型。
为此,你需要在 Eloquent 模型中包含 Solbeg\FilesManager\ModelFilesTrait
并添加 filesAttributes()
方法。
示例
use Solbeg\FilesManager\ModelFilesTrait; class Product extends ...\Eloquent\Model { use ModelFilesTrait; protected $fillable = [..., 'logo_photo', ...]; protected function filesAttributes() { return [ // attribute name => the name of context 'logo_photo' => 'product-logo', // or context config right here //'logo_photo' => [ // 'formats' => [ // 'thumbnail' => 'image/thumb: width = 200, height = 300', // ], // // 'validate' => [ // 'types' => 'image/jpeg, image/png', // 'extensions' => ['jpg', 'jpeg', 'png'], // // ... // ], //], ]; } }
用法
在你的控制器中
$product = new Product; $product->fill($request->all()); // File from input (if passed) will be saved automatically in `public/uploads/product-logo/hashed-subfolder-123/some-hashed-name.jpg` // Formatted 'thumbnail' version (for example) will be generated on fly and saved in `public/uploads/product-logo/hashed-subfolder-123/formats/some-hashed-name.jpg/thumbnail.jpg` // String 'hashed-subfolder-123/some-hashed-name.jpg' will be saved in database in `logo_photo` column. $product->save(); $product = Product::find(1); $product->logo_photo = null; // or '' $product->save(); // file will be removed from database and from disk // returning url to origin file $product->logo_photo->url(); $product->logo_photo->url; $product->logo_photo->href; (string) $product->logo_photo; // returning url of formatted as `thumbnail` versions of file $product->logo_photo->url('thumbnail'); $product->logo_photo->asThumbnail; // Check whether file is empty or not $product->logo_photo->exists(); // whether file exists $product->logo_photo->exists('thumbnail'); // whether formatted file exists $product->logo_photo->isEmpty(/* null or 'thumbnail' */); // reverse of `exists()` method // Fetch other params $product->logo_photo->size(); // size of origin file in bytes $product->logo_photo->size('thumbnail'); // size of formatted file in bytes $product->logo_photo->mimeType(); // MIME type of origin file $product->logo_photo->mimeType('thumbnail'); // MIME type of formatted file $product->logo_photo->lastModified(); // the last modified timestamp of origin file $product->logo_photo->lastModified('thumbnail'); // the last modified timestamp formatted file $product->logo_photo->image()->width(); // origin image width in pixels $product->logo_photo->image('thumbnail')->width(); // formatted image width in pixels $product->logo_photo->image()->height(); // origin image height in pixels $product->logo_photo->image('thumbnail')->height(); // formatted image height in pixels
在你的 blade 模板中
{{-- Output <img ... /> tag --}} <img src="{{ $product->logo_photo }}" alt='' /> <img src="{{ $product->logo_photo->asThumbnail }}" alt='' /> <img src="{{ $product->logo_photo->url() }}" alt='' /> <img src="{{ $product->logo_photo->url('thumbnail') }}" alt='' /> {{ $product->logo_photo->img() }} <!-- outputs 'img' tag --> {{ $product->logo_photo->img('thumbnail') }} <!-- outputs 'img' tag --> {{ $product->logo_photo->img(null, ['id' => 'some-tag-id']) }} <!-- outputs 'img' tag --> {{ $product->logo_photo->link() }} <!-- outputs 'a' tag --> {{ $product->logo_photo->link('thumbnail') }} <!-- outputs 'a' tag --> {{ $product->logo_photo->link(null, 'Download Title', ['class' => 'some-css-class']) <!-- outputs 'a' tag --> {{ $product->logo_photo->link(null, $product->logo_photo->img('thumbnail')) <!-- outputs link with image --> {{-- Check if file is not empty --}} @if($product->logo_photo->exists()) // ... @endif @if($product->logo_photo->isEmpty()) // ... @endif {{-- Output other characteristics --}} Size: {{ $product->logo_photo->size() }} Mime Type: {{ $product->logo_photo->mimeType() }} ...
在 JSON 动作中
return response()->json($product); /* Returns something like: { // ... "logo_photo": { "url": "http://...", "path" => "0001/...", "formats": { "thumbnail": "http://...", "small": "http://...", ... } }, // ... }
你可以在这里了解更多关于将文件导出到数组和 JSON 的信息:这里;阅读有关如何配置必须导出哪些文件属性的说明。
请求验证
当你使用 laravel 的验证系统 验证请求时,你不需要再次编写验证规则。
只需使用 filecontext
验证器,文件将根据上下文验证规则进行验证。
// ... $this->validate($request, [ // ... 'logo_photo' => 'required|filecontext:product-logo', 'some_attribute' => 'filecontext:App\Models\SomeModel@some_attribute', // ... ]); // ...
如果文件有效,但任何其他属性验证失败,则上传的文件将保存在临时存储中。因此,你可以在你的表单中使用 old($fileAttribute)
。
@if (old('logo_photo')) <input type='hidden' name='logo_photo' value='{{ old('logo_photo')->relativePath() }}' /> <img src='{{ old('logo_photo')->url() }}' /> @endif
开发中的功能
// in blade @img($product->logo_photo) @img($product->logo_photo, 'thumbnail')
文档
了解更多关于