g-varlamov/laravel-stapler

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

dev-master 2024-05-24 07:36 UTC

This package is auto-updated.

Last update: 2024-09-24 08:33:03 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,至5.4(5.4是这个包将官方支持的最后一个Laravel版本)。由于Eloquent最近引入的不一致性/更改,以及Laravel现在带有Disks/Flysystem支持,我决定不尝试维护这个包的未来版本。由于有些人已经在他们的Laravel > 5.4项目中使用它,我没有添加对Laravel <= 5.4的严格要求。如果您想在新的Laravel版本中使用此包,您可以在自己的风险下这样做。

如果您将在文件上传过程中进行图像处理,您还需要在您的php环境中安装GD、Gmagick或Imagick(根据您的喜好)。

安装

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,向用户表添加了以下字段

  • (字符串) avatar_file_name
  • (整数) avatar_file_size
  • (字符串) avatar_content_type
  • (时间戳) 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请求之前,请查看Stapler包的故障排除部分。您遇到的问题中有很多(如果不是全部)可能与基本的Stapler包有关,并且已经在那里得到了解决。

贡献

此包始终欢迎贡献

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