mohorev/yii2-upload-behavior
Composer 安装命令:
composer require mohorev/yii2-upload-behavior
包简介
Upload behavior for Yii 2
README 文档
README
This behavior automatically uploads file and fills the specified attribute with a value of the name of the uploaded file. This code is inspired by, but not derived from, https://github.com/yii-dream-team/yii2-upload-behavior.
Installation
The preferred way to install this extension via composer.
Either run
php composer.phar require --prefer-dist mohorev/yii2-upload-behavior "*"
or add this code line to the require section of your composer.json file:
"mohorev/yii2-upload-behavior": "*"
Usage
Upload file
Attach the behavior in your model:
class Document extends ActiveRecord { /** * @inheritdoc */ public function rules() { return [ ['file', 'file', 'extensions' => 'doc, docx, pdf', 'on' => ['insert', 'update']], ]; } /** * @return \yii\db\ActiveQuery */ public function getCategory() { return $this->hasOne(Category::class, [ 'id' => 'id_category' ]); } /** * @inheritdoc */ function behaviors() { return [ [ 'class' => UploadBehavior::class, 'attribute' => 'file', 'scenarios' => ['insert', 'update'], 'path' => '@webroot/upload/docs/{category.id}', 'url' => '@web/upload/docs/{category.id}', ], ]; } }
Set model scenario in controller action:
class Controller extends Controller { public function actionCreate($id) { $model = $this->findModel($id); $model->setScenario('insert'); // Note! Set upload behavior scenario. ... ... } }
Example view file:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> <?= $form->field($model, 'image')->fileInput() ?> <div class="form-group"> <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?>
Upload image and create thumbnails
Thumbnails processing requires yiisoft/yii2-imagine to be installed.
Attach the behavior in your model:
class User extends ActiveRecord { /** * @inheritdoc */ public function rules() { return [ ['image', 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['insert', 'update']], ]; } /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => \mohorev\file\UploadImageBehavior::class, 'attribute' => 'image', 'scenarios' => ['insert', 'update'], 'placeholder' => '@app/modules/user/assets/images/userpic.jpg', 'path' => '@webroot/upload/user/{id}', 'url' => '@web/upload/user/{id}', 'thumbs' => [ 'thumb' => ['width' => 400, 'quality' => 90], 'preview' => ['width' => 200, 'height' => 200], 'news_thumb' => ['width' => 200, 'height' => 200, 'bg_color' => '000'], ], ], ]; } }
Example view file:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> <div class="form-group"> <div class="row"> <div class="col-lg-6"> <!-- Original image --> <?= Html::img($model->getUploadUrl('image'), ['class' => 'img-thumbnail']) ?> </div> <div class="col-lg-4"> <!-- Thumb 1 (thumb profile) --> <?= Html::img($model->getThumbUploadUrl('image'), ['class' => 'img-thumbnail']) ?> </div> <div class="col-lg-2"> <!-- Thumb 2 (preview profile) --> <?= Html::img($model->getThumbUploadUrl('image', 'preview'), ['class' => 'img-thumbnail']) ?> </div> </div> </div> <?= $form->field($model, 'image')->fileInput(['accept' => 'image/*']) ?> <div class="form-group"> <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?>
Behavior Options
- attribute - The attribute which holds the attachment
- scenarios - The scenarios in which the behavior will be triggered
- instanceByName - Getting file instance by name, If you use UploadBehavior in
RESTfullapplication and you do not need a prefix of the model name, set the propertyinstanceByName = false, default value isfalse - path - the base path or path alias to the directory in which to save files.
- url - the base URL or path alias for this file
- generateNewName - Set true or anonymous function takes the old filename and returns a new name, default value is
true - unlinkOnSave - If
truecurrent attribute file will be deleted, default value istrue - unlinkOnDelete - If
truecurrent attribute file will be deleted after model deletion. - deleteOriginalFile - Only for UploadImageBehavior. If
trueoriginal image file will be deleted after thumbs generating, default value isfalse.
Attention!
It is prefered to use immutable placeholder in url and path options, other words try don't use related attributes that can be changed. There's bad practice. For example:
class Track extends ActiveRecord { public function getArtist() { return $this->hasOne(Artist::class, [ 'id' => 'id_artist' ]); } public function behaviors() { return [ [ 'class' => UploadBehavior::class, 'attribute' => 'image', 'scenarios' => ['default'], 'path' => '@webroot/uploads/{artist.slug}', 'url' => '@web/uploads/{artist.slug}', ], ]; } }
If related model attribute slug will change, you must change folders' names too, otherwise behavior will works not correctly.
mohorev/yii2-upload-behavior 适用场景与选型建议
mohorev/yii2-upload-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 275.11k 次下载、GitHub Stars 达 128, 最近一次更新时间为 2018 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「file」 「upload」 「extension」 「Behavior」 「resize」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mohorev/yii2-upload-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mohorev/yii2-upload-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mohorev/yii2-upload-behavior 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
The Yii2 extension uses jQuery PrettyPhoto and OwlCarousel js and makes image galary from php array of structure defined.
Laravel Media Popup to upload/view the files
PMVC Plugin for File information
统计信息
- 总下载量: 275.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 128
- 点击次数: 22
- 依赖项目数: 14
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2018-05-05