a7madev/laravel-stapler

为 Laravel 框架提供易于使用的文件上传管理。

v1.0.08 2016-05-24 06:44 UTC

This package is not auto-updated.

Last update: 2024-09-26 00:44:37 UTC


README

Latest Stable VersionTotal Downloads Latest Unstable Version License

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/staplerapp/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);
	}
}

确保在您的模型中调用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();

这将确保数据库表记录中相应的附件字段被清除,并且当前文件将从存储中删除。数据库表记录本身不会被销毁,可以按需正常使用(或甚至可以分配新的文件上传)。

命令

fasten

此包提供了一个fasten命令,可以用于为现有表生成添加图像文件字段的迁移。此命令的方法签名如下:php artisan stapler:fasten <tablename> <attachment>

在上面的快速入门示例中,调用php artisan stapler:fasten users avatar然后调用php artisan migrate向users表添加了以下字段

  • (string) avatar_file_name
  • (integer) avatar_file_size
  • (string) avatar_content_type
  • (timestamp) avatar_updated_at

refresh

可以使用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"

故障排除

在提交问题或创建pull request之前,请查看Stapler包的故障排除部分。您遇到的问题很可能与基本Stapler包有关,并且已经在那里得到解决。

贡献

此包始终欢迎贡献

  • 主分支将始终包含最新的工作(错误修复、新功能等),但它可能并不总是稳定的;请自行承担风险。每次新的标记版本都将来自主分支上的工作,一旦事情稳定下来等。