desmart/files

此包已被废弃,不再维护。没有建议的替代包。
此包最新版本(2.3.0)没有提供许可证信息。

Laravel 文件管理

2.3.0 2016-10-25 20:44 UTC

README

此包以友好的DDD方式处理文件上传。

安装

在控制台运行

composer require desmart/files:2.0.*

Laravel

DeSmart\Files\ServiceProvider\ServiceProvider 添加到提供者列表。

在控制台运行

php artisan vendor:publish
php artisan migrate

Lumen

将此行添加到 bootstrap/app.php 文件

$app->register('DeSmart\Files\ServiceProvider\LumenServiceProvider');

在控制台运行

cp vendor/desmart/files/database/migrations/* database/migrations/
cp vendor/desmart/files/config/desmart_files.php config/desmart_files.php
php artisan migrate

配置

存储

此包使用 Laravel 的存储机制。默认情况下,包使用 upload 磁盘,需要在 config/filesystems.php 中定义。

<?php
// config/filesystems.php
return [
    'disks' => [
        'upload' => [
            'driver' => 'local',
            'root'   => public_path('upload'),        
        ],
    ],
];

映射器

在保存文件之前,可以使用映射器进行映射。映射器接收生成的 FileEntity 并可以更改其属性。基于实体数据,文件将保存到文件系统和数据库中。

映射器必须实现 DeSmart\Files\Mapper\MapperInterface

自定义文件实体类

默认情况下,包使用 DeSmart\Files\Entity\FileEntity。这可以在 desmart_files.file_entity_class 配置条目中更改。

示例

从上传存储文件

<?php
$file = \Request::file('file');
$source = new \DeSmart\Files\FileSource\UploadedFileSource($file);

// I'm assuming that Manager instance will be injected by Laravel Container
$manager = app('DeSmart\Files\Manager');

// Here we have the FileEntity instance
// File is saved on the filesystem and in the database
$entity = $manager->store($source);

// from here you have save the relation with other entity
// this is just example!

$user->addFile($entity);
$user->save();

从存储中删除文件

此方法仅在给定的 FileEntityfile_records 表中没有条目时才有效。

<?php
$file = new FileEntity; // $file should be obtained in different way (e.g through a relation)

$manager = app('DeSmart\Files\Manager');
$manager->remove($file);