codesleeve/laravel-stapler

为 Laravel 框架提供简单的文件上传管理。

v1.0.09 2017-10-21 19:45 UTC

This package is not auto-updated.

Last update: 2024-09-13 18:02:11 UTC


README

Latest Stable VersionTotal Downloads Latest Unstable Version License

Laravel-Stapler 是一个基于 Stapler 的文件上传包,适用于 Laravel 框架。它提供了一套完整的 Laravel 命令、迁移生成器和基于 Stapler 包的级联包配置。它还以对 Laravel 非常合适的默认值启动 Stapler。如果您想使用 Stapler 与 Laravel,强烈建议您使用此包来实现。

Laravel-Stapler 由 Travis Bennett 创建。

要求

此包目前需要 php >= 5.4 以及 Laravel >= 4,最高到 5.4(5.4 是此包将官方支持的 Laravel 的最后一个版本)。由于最近在 Eloquent 中引入的不一致性/更改,以及 Laravel 现在自带了 Disks/Flysystem 支持,我决定不尝试维护此包以支持 Laravel 的未来版本。由于有些人已经在他们的 Laravel > 5.4 项目中使用它,因此我没有添加对 Laravel <= 5.4 的硬性要求。如果您想在 Laravel 的新版本中使用此包,请自行承担风险。

如果您将在文件上传过程中执行图像处理,您还需要安装 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配置中,现在您不需要传递'Bucket'和'ACL',而是需要传递一个包含这些值的单个's3_object_config'数组(这由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向用户表添加了以下字段

  • (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模型的photo附件:php artisan stapler:refresh TestPhoto --attachments="photo"

重新处理ProfilePicture模型的附件列表:php artisan stapler:refresh TestPhoto --attachments="foo, bar, baz, etc"

故障排除

在提交问题或创建拉取请求之前,请查看Stapler包的故障排除部分。很可能您遇到的大多数(如果不是所有)与此包相关的问题都与基础stapler包有关,并且已经在那里得到了解决。

贡献

此包始终欢迎贡献

  • 主分支将始终包含最新工作(错误修复、新功能等),但它可能不一定总是稳定的;请在自己的风险下使用。每个新标记的发布都将来自主分支上的工作,一旦稳定,等等。