承接 artkost/yii2-attachment 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

artkost/yii2-attachment

Composer 安装命令:

composer require artkost/yii2-attachment

包简介

Yii 2 files attachment

README 文档

README

Build Status

This component provide ability to attach and upload

All uploaded files by default stored in database and have TEMPORARY status.

When model with attachments save yourself, all files attached to the model change their status to permanent.

Cli Commands

php yii attachment/manager/clear clears all temporary files from system

How to use

Configure Manager component

return [
    'components' => [
        'attachmentManager' => [
            'class' => 'artkost\attachment\Manager',
            'storageUrl' => '@web/storage',
            'storagePath' => '@webroot/storage',
            'attachmentFileTable' => '{{%attachment_file}}'
        ]
    ]
]

Create your own type of file

namespace app\modules\user\models;

use artkost\attachment\models\ImageFile;

class UserAvatarFile extends ImageFile
{
    const TYPE = 'userProfile';

    //subfolder of storgae folder
    public $path = 'user/profile';
}

Create model that have attachment_id field, and attach behavior to it

ATTENTION: model can have only one instance of behavior

/**
 * Class Profile
 * @package app\modules\user\models
 * User profile model.
 *
 * @property integer $user_id User ID
 * @property string $name Name
 * @property string $surname Surname
 * @property int $avatar_id Avatar //our attachment_id
 * @property boolean $sex
 *
 * @property User $user User
 * @property UserAvatarFile $avatar avatar file
 */
class UserProfile extends ActiveRecord
{

    public static function tableName()
    {
        return '{{%user_profile}}';
    }

    public function behaviors()
    {
        return [
            'attachBehavior' => [
                'class' => AttachBehavior::className(),
                'models' => [
                    'avatar' => [
                        'class' => UserAvatarFile::className()
                    ]
                ]
            ]
        ];
    }

    public function getAvatar()
    {
        // simply helper method with predefined conditions
        return $this->hasOneAttachment('avatar', ['id' => 'avatar_id']);
    }
}

Currently supported only FileAPI upload, but you can add yours.

Add action into controller

namespace app\modules\user\controllers;

use artkost\attachmentFileAPI\actions\UploadAction as FileAPIUpload;
use app\modules\user\models\UserProfile;

/**
 * Profile controller for authenticated users.
 */
class ProfileController extends Controller
{

    public function actions()
    {
        return [
            'fileapi-upload' => [
                'class' => FileAPIUpload::className(),
                'modelClass' => UserProfile::className(),
                'attribute' => 'avatar'
                //'accessCheck' => function($action) {  }
            ]
        ];
    }

}

in action view file you can use widget for upload files

use artkost\attachmentFileAPI\widgets\File as FileAPIWidget;
?>
...
<?= $form->field($model, 'preview')->widget(
    FileAPIWidget::className(),
    [
        'url' => ['upload-preview'],
        'settings' => [
            'autoUpload' => true
        ]
    ]
)->label(false) ?>

统计信息

  • 总下载量: 46
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-14