teamteatime/laravel-filer

此包已被废弃,不再维护。未建议替代包。

一个简单的支持多种关系类型的 Laravel ORM 文件附件解决方案

1.0.2 2016-06-02 16:49 UTC

This package is not auto-updated.

Last update: 2024-04-05 09:14:39 UTC


README

请注意,此包 不是 设计用来处理上传或图像处理的。它仅是为了补充Laravel中已经存在的其他包和工具。如果您正在寻找一个功能更全面的附件解决方案,请查看 CodeSleeve/stapler

安装

步骤 1: 安装包

通过 Composer 安装包

composer require teamteatime/laravel-filer

将服务提供者添加到您的 config/app.php

TeamTeaTime\Filer\FilerServiceProvider::class,

如果您的应用程序定义了一个通配路由,请确保在您的应用程序服务提供者之前加载此服务提供者。

步骤 2: 发布包文件

运行 vendor:publish 命令以发布 Filer 的迁移

php artisan vendor:publish

步骤 3: 更新数据库

运行迁移

php artisan migrate

步骤 4: 更新模型

通过使用 HasAttachments 特性来为您的模型添加附件支持

class ... extends Eloquent {
    use \TeamTeaTime\Filer\HasAttachments;
}

配置

在大多数情况下,Filer 不需要配置,但在 config/filer.php 中提供了以下选项供您选择

选项 类型 描述 默认
routes 布尔值 确定是否自动定义 filer 的路由。如果您将其设置为 false,则可以在 routes.php 中选择性地使用 \TeamTeaTime\Filer\Filer::routes($router, $namespace) true
route_prefix 字符串 如果启用了路由,则用于所有路由前缀。 files
hash_routes 布尔值 启用本地文件的唯一哈希,以在路由中混淆其 ID。 false
hash_length 字符串 生成本地文件哈希时使用的长度。 40
path 数组 包含存储附件文件的目录的相对和绝对路径。 storage_path('uploads')
append_querystring 布尔值 如果启用,附件 URL 将包含一个包含附件 updated_at 时间戳的查询字符串。这可以防止浏览器加载过时的附件。 true
cleanup_on_delete 布尔值 如果启用,Filer 将尝试删除由已删除附件引用的本地文件。 true

使用

要附加文件或 URL,请在您的模型上使用 attach() 方法。此方法可以接受以下任何一种

...一个 本地文件路径

$user->attach('avatars/1.jpg'); // path relative to your configured storage directory

...一个 SplFileInfo 实例

$photo = Request::file('photo')->move($destinationPath);
$user->attach($photo);

Symfony\Component\HttpFoundation\File\FileSymfony\Component\HttpFoundation\File\UploadedFileIlluminate\Http\UploadedFileSplFileInfo 的扩展,并且 Laravel 请求默认包含后者。

...或一个 URL

$user->attach('http://www.analysis.im/uploads/seminar/pdf-sample.pdf');

您还可以使用选项数组指定一个键(唯一标识附件)、一个标题和/或一个描述

$user->attach('uploads/avatars/1.jpg', ['key' => 'avatar']);
$article->attach($pdf, ['title' => "Event 2015 Guide", 'description' => "The complete guide for this year's event."]);

默认情况下,附件通过 Auth::id() 与用户 ID 关联。您可以在调用时覆盖此操作

$user->attach($photo, ['user_id' => $user->id]);
$article->attach($pdf, ['user_id' => null]);

根据传递给此方法的参数,项目将被存储为 TeamTeaTime\Filer\LocalFileTeamTeaTime\Filer\Url。您可以通过 attachments 关系稍后调用附件。以下提供了示例。

在视图中显示附件列表

@foreach ($article->attachments as $attachment)
<a href="{{ $attachment->getDownloadUrl() }}">{{ $attachment->title }}</a>
<p class="description">{{ $attachment->description }}</p>
@endforeach

通过 ID 或键检索附件

$user->attachments()->find($attachmentId);
$user->findAttachmentByKey('avatar');

访问附件的属性和特定类型的属性

$avatar = $user->findAttachmentByKey('avatar');
$avatar->getUrl();          // the URL to the file (see below)
$avatar->getDownloadUrl();  // the download URL to the file (see below)
$avatar->title;             // the attachment title, if any
$avatar->description;       // the attachment description, if any

// If the attachment is a LocalFile...
$avatar->item->filename;    // the filename, with its extension
$avatar->item->path;        // the path to the directory where the file exists
$avatar->item->mimetype;    // the file's detected MIME type
$avatar->item->size;        // the file size, in bytes
$avatar->item->getFile();   // the Symfony File representation of the file
$avatar->item->hash;        // the unique hash generated for the file (if filer.hash_routes is enabled)

生成 URL

上述的 getUrl()getDownloadUrl() 方法会根据附件类型返回不同的值;如果是本地文件,它们将分别返回 'view' 和 'download' 路由,否则它们将返回所附加的 URL。

对于本地文件,可以通过文件 ID 或哈希生成提供的路由。

route('filer.file.view', $fileId);
route('filer.file.download', $fileId)

请注意,根据文件的 MIME 类型,浏览器可能使用这两个路由中的任何一个开始下载。