mix8872 / yii2-files
文件附件模块
Requires
- php: >=7.1.0
- ext-intl: *
- ext-mbstring: *
- himiklab/yii2-sortable-grid-view-widget: *
- intervention/image: *
- npm-asset/bootstrap-fileinput: ~5.1
- twbs/bootstrap-icons: ^1.10
- yiisoft/yii2-bootstrap5: 2.0.3
README
此模块用于将任何文件附加到您的模型上。
此模块是 mix8872/files-attacher
的新版本,并且与之不兼容!
您可以使用旧的 mix8872/files-attacher 或完全删除它并安装此模块。
未提供迁移机制。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist mix8872/yii2-files
或
"mix8872/yii2-files": "~1.0"
将以下内容添加到您的 composer.json
文件的 require
部分。
然后,您必须通过运行以下命令执行迁移:
yii migrate --migrationPath=@vendor/mix8872/yii2-files/src/migrations
配置
要配置模块,请将以下内容添加到 common 主配置文件中的 modules 部分
Common
'modules' => [ 'files' => [ 'class' => 'mix8872\yiiFiles\Module', 'parameters' => [ // general module parameters 'sizes' => [ // if imgProcessDriver is GD supports only jpeg, png and gif 'galleryMiddle' => ['width' => '880', 'height' => '587', 'model' => ['common\modules\imageslider\models\ImageSlider']], 'galleryPreview' => ['width' => '120', 'height' => '60', 'model' => ['common\modules\imageslider\models\ImageSlider']] ], 'sizesNameBy' => 'template', // or 'key', optional, default 'size' 'sizesNameTemplate' => '_resize_%k-%s', //optional, if sizesNameBy set to 'template' 'imgProcessDriver' => 'gd', //or 'imagick', optional, default 'gd', 'filesNameBy' => 'translit', // optional, by default naming is random string 'savePath' => '/uploads/attachments/', // optional, default save path is '/uploads/attachments/' ], 'attributes' => [ // general attributes configuration, optional 'preview' => [ 'filetypes' => ['image/*'], 'resize' => ['width' => '1024', 'height' => '768'], //optional, if imgProcessDriver is GD supports only jpeg, png and gif ], 'images' => [ 'multiple' => true, 'filetypes' => ['image/*'], ] ] ], // ... other modules definition ],
前端
'modules' => [
...
'files' => [
'as access' => [
'class' => \yii\filters\AccessControl::class,
'rules' => [
[
'allow' => false,
],
],
],
],
],
重要!!!
不要忘记拒绝前端应用程序对模块的访问!!!
后端
'modules' => [
...
'files' => [
'as access' => [
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'controllers' => ['files/default'],
'allow' => true,
'roles' => ['admin']
],
]
],
],
],
在配置中,您可以定义访问控制以防止访问模块的行政部分。
您还可以定义 sizes
以创建上传图像的附加尺寸。
在 resize
定义中,您还可以可选地定义将应用缩放的模型。支持将多个模型定义为数组。
要使用尺寸名称模板,您可以使用 sizesNameTemplate
选项,其中 %k
- 键,%s
- 尺寸。默认为 %s
;
如果定义了 origResize
选项,则将更改原始图像大小。您还可以定义模型数组;
您还可以更改图像驱动程序为 imagick。
通过定义 filesNameBy
选项,您可以将文件命名样式从随机字符串更改为转写文件名,还可以定义 model
属性。
要更改默认保存路径,您可以定义 savePath
选项。路径将从 Web 目录考虑。
用法
作为模型的小部件和行为,可以使用此模块。
首先,您必须以这种方式配置所需模型的行为
在 tags 属性中,您可以定义附加文件的标签,如果定义相同的标签在 delteOld 属性中,则带有这些标签加载的文件将被新添加的文件覆盖。
您还可以在行为中直接配置附件参数
public function behaviors() { return [ 'files' => [ 'class' => \mix8872\yiiFiles\behaviors\FileAttachBehavior::class, 'attributes' => [ 'images' => [ 'multiple' => true, // optional, default false. allow multiple loading 'filetypes' => ['image/*'], // array of mime types of allowed files 'resize' => ['width' => '1024', 'height' => '768'], //optional, for images only ], 'videos' => [ ] ], ], // ... other behaviors ]; }
无论如何,您必须声明具有此名称的 fileAttachBehavior files
public function behaviors() { return [ 'files' => [ 'class' => \mix8872\yiiFiles\behaviors\FileAttachBehavior::class, ... ], // ... other behaviors ]; }
如果您需要批量更新模型及其文件,则可以定义 indexBy
行为属性
并指定将用于索引的父模型属性名称。默认为 id
。
public function behaviors() { return [ 'files' => [ 'class' => \mix8872\yiiFiles\behaviors\FileAttachBehavior::class, 'attributes' => [ ... ], 'indexBy' => 'code', // by default - id ... ], // ... other behaviors ]; }
接下来,您可以添加具有其配置的 widget 模型和 echo 小部件
use mix8872\yiiFiles\widgets\FilesWidget; ?> <!-- ... some view code --> <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> <!-- ['options' => ['enctype' => 'multipart/form-data']] - is IMPORTANT if you use widget as stay alone widget instead input widget --> ... <!-- Preferably widget declaration --> <?= $form->field($model, 'images')->widget(FilesWidget::class, [ 'theme' => FilesWidget::THEME_BROWSE, // optional, or THEME_DRAGDROP or THEME_BROWSE_DRAGDROP 'width' => '100%', // optional, width of dragDrop zone 'height' => '100px', // optional, height of dragDrop zone 'options' => ['class' => 'some-custom-class'] // optional, custom input options ])->label('Custom label') ?> <!-- OR --> <?= FilesWidget::widget([ 'model' => $model, 'attribute' => 'videos', // one of the tags listed in the model 'theme' => FilesWidget::THEME_BROWSE, // or THEME_DRAGDROP or THEME_BROWSE_DRAGDROP 'width' => '100%', // optional, width of dragDrop zone 'height' => '100px', // optional, height of dragDrop zone 'options' => ['class' => 'some-custom-class'] // optional, custom input options ]) ?> ... <?php ActiveForm::end() ?>
!!! 重要!!!
如果您使用独立小部件而不是输入小部件,则可以定义具有 ['options' => ['enctype' => 'multipart/form-data']]
的表单。
如果使用输入小部件,则 multipart/form-data
将自动添加到您的表单中。
您还可以按如下方式通过 URL 将文件附加到模型
$model->attachByUrl(string $tag, string $url);
您可以通过调用以下方法获取模型文件
$files = $model->getFiles('property'); //array of file objects // public function getFiles(string $property, bool $single, bool $asQuery)
- $property - 附件的标签
- $single - 如果为 true - 返回单个附件对象,默认为 false
- $asQuery - 如果为 true - 返回 ActiveQuery 对象,默认为 false
或通过属性访问
$files = $model->property;
如果使用获取文件作为属性,则当属性是多个文件时,你会得到文件对象的数组;如果属性不是单个文件对象时。
事件
在添加、更新和删除文件时,会生成以下事件
- EVENT_FILE_ADD
- EVENT_FILE_UPDATE
- EVENT_FILE_DELETE
为了捕获事件,你可以在你的模型中声明以下函数
在添加文件时触发
public function onFileAdd()
{
error_log('File add');
}
在更新文件时触发
public function onFileUpdate()
{
error_log('File update');
}
在删除文件时触发
public function onFileDelete()
{
error_log('File delete');
}
控制台
即将推出