teamteatime/laravel-filer
最新稳定版本:1.0.2
Composer 安装命令:
composer require teamteatime/laravel-filer
包简介
A simple Laravel ORM file attachment solution supporting multiple relationship types
README 文档
README
Please be aware that this package is not designed to handle uploading or image manipulation. It's simply designed to compliment other packages and tools that already exist in Laravel. If you're looking for a more feature-complete attachments solution, take a look at CodeSleeve/stapler.
Installation
Step 1: Install the package
Install the package via Composer:
composer require teamteatime/laravel-filer
Add the service provider to your config/app.php:
TeamTeaTime\Filer\FilerServiceProvider::class,
If your app defines a catch-all route, make sure you load this service provider before your app service providers.
Step 2: Publish the package files
Run the vendor:publish command to publish Filer's migrations:
php artisan vendor:publish
Step 3: Update your database
Run your migrations:
php artisan migrate
Step 4: Update your models
Add attachment support to your models by using the HasAttachments trait:
class ... extends Eloquent { use \TeamTeaTime\Filer\HasAttachments; }
Configuration
Filer requires no configuration out of the box in most cases, but the following options are available to you in config/filer.php:
| Option | Type | Description | Default |
|---|---|---|---|
| routes | Boolean | Determines whether or not to automatically define filer's routes. If you set this to false, you can optionally use \TeamTeaTime\Filer\Filer::routes($router, $namespace) in your routes.php. |
true |
| route_prefix | string | If routes are enabled, this is used for all route prefixes. | files |
| hash_routes | Boolean | Enables unique hashes for local files to obfuscate their IDs in routes. | false |
| hash_length | string | The length to use when generating hashes for local files. | 40 |
| path | Array | Contains the relative and absolute paths to the directory where your attachment files are stored. | storage_path('uploads') |
| append_querystring | Boolean | If enabled, attachment URLs include a querystring containing the attachment's updated_at timestamp. This prevents out of date attachments from being loaded by the browser. | true |
| cleanup_on_delete | Boolean | If enabled, Filer will attempt to delete local files referenced by deleted attachments. | true |
Usage
To attach a file or URL, use the attach() method on your model. This method will accept any of the following:
...a local file path
$user->attach('avatars/1.jpg'); // path relative to your configured storage directory
...an instance of SplFileInfo
$photo = Request::file('photo')->move($destinationPath); $user->attach($photo);
Symfony\Component\HttpFoundation\File\File,Symfony\Component\HttpFoundation\File\UploadedFileandIlluminate\Http\UploadedFileare extensions ofSplFileInfoand Laravel Requests contain the latter by default.
...or a URL
$user->attach('http://www.analysis.im/uploads/seminar/pdf-sample.pdf');
You can also specify a key (which uniquely identifies the attachment), a title, and/or a description using the options array:
$user->attach('uploads/avatars/1.jpg', ['key' => 'avatar']);
$article->attach($pdf, ['title' => "Event 2015 Guide", 'description' => "The complete guide for this year's event."]);
By default, attachments are associated with user IDs using Auth::id(). You can override this at call time:
$user->attach($photo, ['user_id' => $user->id]);
$article->attach($pdf, ['user_id' => null]);
Depending on what you pass to this method, the item will be stored as either a TeamTeaTime\Filer\LocalFile or a TeamTeaTime\Filer\Url. You can later call on attachments via the attachments relationship. Examples are provided below.
Displaying a list of attachments in a view
@foreach ($article->attachments as $attachment)
<a href="{{ $attachment->getDownloadUrl() }}">{{ $attachment->title }}</a>
<p class="description">{{ $attachment->description }}</p>
@endforeach
Retrieving an attachment by ID or key
$user->attachments()->find($attachmentId);
$user->findAttachmentByKey('avatar');
Accessing an attachment's properties and type-specific properties
$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)
Generating URLs
The getUrl() and getDownloadUrl() methods above will return different values based on the attachment type; if it's a local file, they will return the 'view' and 'download' routes respectively, otherwise they'll return the URL that was attached.
For local files, the provided routes can be generated with a file ID or hash:
route('filer.file.view', $fileId);
route('filer.file.download', $fileId)
Note that depending on the file's MIME type, the browser may begin a download with both of these routes.
teamteatime/laravel-filer 适用场景与选型建议
teamteatime/laravel-filer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.58k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2015 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「files」 「laravel」 「attachments」 「laravel-5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 teamteatime/laravel-filer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 teamteatime/laravel-filer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 teamteatime/laravel-filer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An HTML5 upload field for the CMS and frontend forms.
Allow adding attachments to any document
Restore media attachments using WP CLI
A very simple yet useful helper class to handle PHP file uploads.
The flysystem adapter for yandex disk rest api.
File attachments for laravel.
统计信息
- 总下载量: 2.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-29