maxmirazh33/yii2-uploadable-file

用于上传文件的Yii2扩展

v2.2.2 2020-03-03 06:57 UTC

This package is auto-updated.

Last update: 2024-08-29 03:36:44 UTC


README

用于上传文件的Yii2扩展

Latest Version Software License Quality Score Total Downloads

安装

安装此扩展的首选方式是通过 composer

可以运行

php composer.phar require --prefer-dist maxmirazh33/yii2-uploadable-file "*"

或者在您的 composer.json 文件的 require 部分添加

"maxmirazh33/yii2-uploadable-file": "*"

to

使用

扩展安装完成后,只需在您的代码中使用即可

在您的模型中

public function behaviors()
    {
        return [
            [
                'class' => \maxmirazh33\file\Behavior::className(),
                'savePathAlias' => '@web/files/',
                'urlPrefix' => '/files/',
                'attributes' => [
                    'image' => [
                        'savePathAlias' => '@web/images/',
                        'urlPrefix' => '/images/',
                    ],
                    'file',
                ],
            ],
        //other behaviors
        ];
    }

使用规则验证属性。

如果您的需要完美的文件输入,那么在您的视图文件中

echo $form->field($model, 'file')->widget('maxmirazh33\file\Widget');

之后,在您的视图中

echo Html::a('myCoolFile', $model->getFileUrl('file'));

如果您使用高级应用模板并且此行为附加在后端模型中,那么在前端模型中添加 trait

use \maxmirazh33\file\GetFileUrlTrait

并使用 getFileUrl() 方法为前端模型。