roboticsexpert / laravel-stapler
为Laravel框架提供易于使用的文件上传管理。
Requires
- php: >=5.4.0
- laravel/framework: 4.*|5.*
- roboticsexpert/stapler: 1.2.*
This package is not auto-updated.
Last update: 2024-09-18 19:45:42 UTC
README
Laravel-Stapler是一个基于Stapler的文件上传包,适用于Laravel框架。它提供了完整的Laravel命令、迁移生成器和基于Stapler包的级联包配置。它还使用非常合理的默认值来启动Stapler以配合Laravel使用。如果您想要在Laravel中使用Stapler,强烈建议您使用此包来做到这一点。
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配置中,现在您需要传递一个包含'key'、'secret'、'region'和'scheme'选项的单个's3_client_config'数组,而不是传递这些选项。这些选项将直接传递到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](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); } }
确保在您的模型中调用
hasAttachedFile()
方法之前调用parent::__construct()
。
从命令行,使用迁移生成器
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();
这将确保相应的附件字段在数据库表记录中被清除,当前文件将从存储中删除。数据库表记录本身不会被销毁,可以正常使用(或者甚至可以按需分配新的文件上传)。
命令
固定
此包提供了一个固定
命令,可用于为现有表添加图像文件字段生成迁移。此命令的签名如下:php artisan stapler:fasten <tablename> <attachment>
在上面的快速入门示例中,调用php artisan stapler:fasten users avatar
,然后调用php artisan migrate
,向用户表添加了以下字段
- (字符串) avatar_file_name
- (整数) avatar_file_size
- (字符串) avatar_content_type
- (时间戳) avatar_updated_at
刷新
刷新
命令可用于重新处理模型附件中的上传图像。它通过在模型的每个附件上(或仅在某些特定的附件上)调用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"
故障排除
在提交问题或创建pull请求之前,请查看Stapler包的故障排除部分。有许多(如果不是全部)您与此包相关的错误可能与基本的Stapler包有关,并且已经在那里得到了解决。
贡献
此包始终欢迎贡献
- 主分支将始终包含最新的工作(错误修复、新功能等),但它可能并不总是稳定的;请自行承担风险。每个新的标记版本都将来自主分支上的工作,一旦事情稳定下来,等等。