arter/amos-attachments

该包最新版本(1.6.0)没有提供许可证信息。

文件上传和附件模型扩展

安装: 585

依赖: 11

建议者: 0

安全: 0

类型:yii2-extension

1.6.0 2024-04-03 08:50 UTC

This package is auto-updated.

Last update: 2024-09-07 09:49:33 UTC


README

文件上传和附件模型扩展

演示

您可以在krajee网站上查看演示

安装

  1. 通过composer安装此扩展是首选方式。

运行以下命令

composer require arter/amos-attachments

或者在您的composer.json文件的require部分添加以下内容

"arter/amos-attachments": ">=1.0"

  1. 将模块添加到您的common主配置中
<?php
'aliases' => [
    '@file' => dirname(__DIR__),
],
'modules' => [
    'attachments' => [
        'class' => 'arter\amos\attachments\FileModule',
        'webDir' => 'files',
        'tempPath' => '@common/uploads/temp',
        'storePath' => '@common/uploads/store',
        // 'tableName' => '{{%attach_file}}' // Optional, default to 'attach_file'
    ],
],

同时,将这些行添加到您的控制台配置中

<?php
'controllerMap' => [
    'attachments' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => '@amos_attachments/migrations'
    ],
],
  1. 应用迁移
php yii migrate/up --migrationPath=@vendor/arter/amos-attachments/src/migrations
  1. 将行为附加到您的模型上(请确保您的模型具有“id”属性)
<?php
use yii\helpers\ArrayHelper;

/**
 * Adding the file behavior
 */
public function behaviors()
{
    return ArrayHelper::merge(parent::behaviors(), [
        'fileBehavior' => [
            'class' => \file\behaviors\FileBehavior::className()
        ]
    ]);
}

/**
 * Add the new fields to the file behavior
 */
public function rules()
{
    return ArrayHelper::merge(parent::rules(), [
        [['my_field_multiple_files', 'my_field_single_file'], 'file'],
    ]);
}
  1. 确保您已经将'enctype' => 'multipart/form-data'添加到ActiveForm选项中

  2. 确保您已在模块规则中指定了maxFiles,并在AttachmentsInput上指定了maxFileCount到您想要的数字

  3. 您现在可以使用它了,查看如何使用