mix8872 / files-attacher
文件附加模块
Requires
- php: >=7.1.0
- ext-intl: *
- ext-mbstring: *
- himiklab/yii2-sortable-grid-view-widget: *
- intervention/image: *
This package is not auto-updated.
Last update: 2024-10-02 11:13:49 UTC
README
用于将任何文件附加到模型的文件附加模块。
安装
安装此扩展的首选方法是通过composer。
运行
php composer.phar require --prefer-dist mix8872/files-attacher
或添加
"mix8872/files-attacher": "~1.0"
到你的composer.json
文件的require
部分。
然后,你必须通过运行以下命令执行迁移
yii migrate --migrationPath=@vendor/mix8872/files-attacher/src/migrations
配置
要配置模块,请将以下内容添加到通用主配置的模块部分
通用
'modules' => [ 'filesAttacher' => [ 'class' => 'mix8872\filesAttacher\Module', 'as access' => [ 'class' => 'yii\filters\AccessControl', 'rules' => [ [ 'controllers' => ['filesAttacher/default'], 'allow' => true, 'roles' => ['admin'] ], ] ], 'parameters' => [ 'imageResize' => [ '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', optionally, default 'size' 'sizesNameTemplate' => '_resize_%k-%s', //optionally, if sizesNameBy set to 'template' 'origResize' => ['width' => '1024', 'height' => '768', 'model' => ['common\modules\imageslider\models\ImageSlider']], //optionally 'imgProcessDriver' => 'imagick', //or 'imagick', optionally, default 'gd', 'filesNameBy' => 'translit', // optionally, by default naming is random string 'savePath' => '/uploads/attachments/', // optionally, default save path is '/uploads/attachments/' ], ], // ... other modules definition ],
在配置中,您可以定义访问控制以防止访问模块的行政部分。
您还可以定义imageResize
以创建上传图像的额外大小。
在imageResize
定义中,您还可以可选地定义将应用缩放的模型。支持定义多个模型作为数组。
要使用大小名称模板,您可以定义sizesNameTemplate
选项,其中%k
- 键,%s
- 大小。默认为%s
;
如果定义了origResize
选项,则原始图像大小将更改。您还可以定义模型数组;
您还可以更改图像驱动程序为imagick。
通过定义filesNameBy
选项,您可以更改文件命名风格从随机字符串到转写文件名,您还可以定义model
属性。
要更改默认保存路径,您可以定义savePath
选项。路径将从网络目录考虑。
用法
可以使用模块作为模型的控件和行为。
首先,您必须以这种方式配置所需模型的控件
public function behaviors() { return [ [ 'class' => \mix8872\filesAttacher\behaviors\FileAttachBehavior::class, 'tags' => ['images','videos'], 'deleteOld' => [] ], // ... other behaviors ]; }
在标签属性中,您可以定义附加文件的标签,如果定义了相同的标签在delteOld属性中,则使用此标签加载的文件将被新添加的文件覆盖。
接下来,您可以添加带有其配置的控件模型和echo控件
use mix8872\filesAttacher\widgets\FilesWidget; // ... you view code <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); // IMPORTANT ?> ... <?= FilesWidget::widget([ 'model' => $model, 'tag' => 'videos', // one of the tags listed in the model 'multiple' => true, // true or false. allow multiple loading 'filetypes' => ['video/*'], // array of mime types of allowed files ]) ?> <?php ActiveForm::end() ?>
重要!您可以在表单中定义multipart/form-data编码类型!
您可以通过调用方法获取模型文件
$files = $model->getFiles('tag'); //array of file objects
public function getFiles(string $tag, bool $single, bool $asQuery)
$tag - 附加文件的标签 $single - 如果为true - 返回单个附件对象 $asQuery - 如果为true - 返回ActiveQuery对象