mix8872/files-attacher

文件附加模块

安装: 184

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

语言:JavaScript

类型:yii2-extension

1.10.3 2020-08-11 13:19 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对象