gerizal / laravel-stapler
为Laravel框架提供简单易用的文件上传管理。
Requires
- php: >=5.4.0
- gerizal/stapler: 3.0.0
- laravel/framework: 4.*|5.*
This package is auto-updated.
Last update: 2024-09-14 19:31:30 UTC
README
Laravel-Stapler是基于Stapler的文件上传包,适用于Laravel框架。它提供了一套完整的Laravel命令、迁移生成器以及基于Stapler包的级联包配置。它还通过非常合理的默认值启动Stapler以用于Laravel。如果您想使用Stapler与Laravel一起使用,强烈建议您使用此包。
Laravel-Stapler由Travis Bennett创建。
要求
此包目前需要php >= 5.4以及Laravel >= 4。如果您在文件上传过程中需要进行图像处理,您还需要安装GD、Gmagick或Imagick(根据您的喜好)作为php环境的一部分。
安装
Laravel-Stapler以Composer包的形式发布,这是在您的应用程序中使用它的正确方式。
使用Composer安装此包。编辑您项目中的composer.json
文件,以要求codesleeve/laravel-stapler
。
"require": { "laravel/framework": "4.*", "codesleeve/laravel-stapler": "1.0.*" }
完成此操作后,下一步是添加服务提供者。
对于Laravel 4,打开app/config/app.php
,并在提供者数组中添加一个新条目
'Codesleeve\LaravelStapler\Providers\L4ServiceProvider'
对于Laravel 5,打开config/app.php
,并在提供者数组中添加一个新条目
'Codesleeve\LaravelStapler\Providers\L5ServiceProvider'
弃用
截至1.0.04,'Codesleeve\LaravelStapler\LaravelStaplerServiceProvider'服务提供者已被弃用(此提供者将在下一个主要版本中删除)。相反,您现在应该使用您使用的特定版本的Laravel对应的提供者。
migrating-from-Stapler-v1.0.0-Beta4
如果您已经在Laravel应用程序中使用了Stapler(在v1.0.0-Beta4之前),现在您需要使用此包。卸载Stapler(从composer.json中移除,移除服务提供者等)并按照上面的说明安装此包。安装后,您可能需要在应用程序中进行以下更改
-
在您的使用Stapler的模型中,将
use Codesleeve\Stapler\Stapler
更改为use Codesleeve\Stapler\ORM\EloquentTrait
。您的模型还需要实现Codesleeve\Stapler\ORM\StaplerableInterface
。 -
如果您发布了stapler的配置,您需要将配置文件夹重命名从
app/config/packages/codesleeve/stapler
到app/config/packages/codesleeve/laravel-stapler
。 -
图像处理库现在通过Imagine Image包的完整类名引用(例如
gd
现在通过Imagine\Gd\Imagine
引用)。 -
在您的s3配置中,现在您需要传递一个包含这些选项(以及您可能需要的任何其他选项)的单个's3_client_config'数组,而不是传递'key'、'secret'、'region'和'scheme'选项。这些选项将直接传递给s3ClientFactory以创建S3客户端。将参数作为数组传递现在允许您根据需要配置您的s3客户端(对于给定的模型/附件)。请参阅:http://docs.aws.amazon.com/aws-sdk-php/guide/latest/configuration.html#client-configuration-options
-
在您的s3配置中,您现在需要传递一个包含这些值的单个's3_object_config'数组,而不是传递'Bucket'和'ACL'(这是由S3Client::putObject()方法使用的)。请参阅:http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_putObject
-
':laravel_root'的插值已被更改为':app_root'
快速入门
在您的应用程序根目录(最可能是public文件夹)中,创建一个名为system的文件夹,并授予您的应用程序对该文件夹的写权限。为此,我们假设存在一个现有的User
模型,我们将向其中添加一个头像图片。
在您的模型中
use Codesleeve\Stapler\ORM\StaplerableInterface; use Codesleeve\Stapler\ORM\EloquentTrait; class User extends Eloquent implements StaplerableInterface { use EloquentTrait; // Add the 'avatar' attachment to the fillable array so that it's mass-assignable on this model. protected $fillable = ['avatar', 'first_name', 'last_name']; public function __construct(array $attributes = array()) { $this->hasAttachedFile('avatar', [ 'styles' => [ 'medium' => '300x300', 'thumb' => '100x100' ] ]); parent::__construct($attributes); } }
请确保在您的模型的
parent::__construct()
之前调用hasAttachedFile()
方法。
从命令行使用迁移生成器
php artisan stapler:fasten users avatar php artisan migrate
在您的新的视图中
<?= Form::open(['url' => action('UsersController@store'), 'method' => 'POST', 'files' => true]) ?> <?= Form::input('first_name') ?> <?= Form::input('last_name') ?> <?= Form::file('avatar') ?> <?= Form::submit('save') ?> <?= Form::close() ?>
在您的控制器中
public function store() { // Create and save a new user, mass assigning all of the input fields (including the 'avatar' file field). $user = User::create(Input::all()); }
在您的显示视图中
<img src="<?= $user->avatar->url() ?>" > <img src="<?= $user->avatar->url('medium') ?>" > <img src="<?= $user->avatar->url('thumb') ?>" >
要分离(重置)一个文件,只需将常量STAPLER_NULL分配给附件并保存即可)
$user->avatar = STAPLER_NULL; $user->save();
这将确保数据库表记录中对应的附件字段被清除,并且当前文件将从存储中删除。数据库表记录本身不会被销毁,可以正常使用(或根据需要分配新的文件上传)。
命令
固定
此包提供了一个fixed
命令,可用于为现有表生成添加图像文件字段的迁移。此命令的方法签名如下:php artisan stapler:fixed <tablename> <attachment>
在上面的快速入门示例中,调用php artisan stapler:fixed users avatar
后,再调用php artisan migrate
,将以下字段添加到users表中
- (字符串) avatar_file_name
- (整数) avatar_file_size
- (字符串) avatar_content_type
- (时间戳) avatar_updated_at
刷新
可以使用refresh
命令重新处理模型附件中上传的图像。它通过在模型的每个附件(或特定附件)上调用reprocess()方法来实现。这在已经为附件上传了文件时添加新样式到现有附件时非常有用。
重新处理ProfilePicture模型的全部附件:php artisan stapler:refresh ProfilePicture
仅重新处理ProfilePicture模型上的照片附件:php artisan stapler:refresh TestPhoto --attachments="photo"
重新处理ProfilePicture模型上的附件列表:php artisan stapler:refresh TestPhoto --attachments="foo, bar, baz, etc"
故障排除
在提交问题或创建拉取请求之前,请查看Stapler包的故障排除部分。很可能您遇到的许多(如果不是全部)问题都与基础Stapler包相关,并已在那里得到解决。
贡献
此包始终欢迎贡献
- 主分支将始终包含最新工作(错误修复、新功能等),但它可能并不总是稳定的;自行承担风险。每次新的标签版本都会来自主分支上的工作,一旦事情稳定下来,等等。