项目作者: fredyns

项目描述 :
Yii2 Extension to upload models attachment into Flysystem (S3)
高级语言: PHP
项目地址: git://github.com/fredyns/yii2-attachments.git
创建时间: 2021-03-19T15:00:40Z
项目社区:https://github.com/fredyns/yii2-attachments

开源协议:

下载


Yii2 Attachments

Latest Stable Version
License
Total Downloads

Upload models attachment to Flysystem

Demo

You can see the demo of upload input on the krajee website

Installation

  1. install yii2-flysystem and filesystem of your choice.

    Please look closely at its documentation for installation. (it may get some update)

  1. The preferred way to install this extension is through composer.

    Either run

    1. php composer.phar require fredyns/yii2-attachments "dev-master"

    or add

    1. "fredyns/yii2-attachments": "dev-master"

    to the require section of your composer.json file.

  2. Add module to common/config/main.php (advanced template)

    for basic app you should add to both config/web.php & config/console.php.

    1. 'modules' => [
    2. ...
    3. 'attachments' => [
    4. 'class' => fredyns\attachments\Module::class,
    5. 'rules' => [ // Rules according to the FileValidator
    6. 'maxFiles' => 10, // Allow to upload maximum 3 files, default to 3
    7. 'mimeTypes' => 'image/png', // Only png images
    8. 'maxSize' => 1024 * 1024 // 1 MB
    9. ],
    10. 'tableName' => '{{%attachments}}' // Optional, default to 'attach_file'
    11. 'filesystem' => 'awss3Fs' // you can change though
    12. ]
    13. ...
    14. ]
  3. Apply migrations

  1. ```php
  2. 'controllerMap' => [
  3. ...
  4. 'migrate' => [
  5. 'class' => 'yii\console\controllers\MigrateController',
  6. 'migrationNamespaces' => [
  7. 'fredyns\attachments\migrations',
  8. ],
  9. ],
  10. ...
  11. ],
  12. ```
  13. ```
  14. php yii migrate/up
  15. ```
  1. Attach behavior to your model (be sure that your model has “id” property)

    1. public function behaviors()
    2. {
    3. return [
    4. ...
    5. 'fileBehavior' => [
    6. 'class' => \fredyns\attachments\behaviors\FileBehavior::class,
    7. ]
    8. ...
    9. ];
    10. }
  2. Make sure that you have added 'enctype' => 'multipart/form-data' to the ActiveForm options

  3. Make sure that you specified maxFiles in module rules and maxFileCount on AttachmentsInput to the number that you want

Usage

  1. In the form.php of your model add file input

    1. <?= \fredyns\attachments\components\AttachmentsInput::widget([
    2. 'id' => 'file-input', // Optional
    3. 'model' => $model,
    4. 'options' => [ // Options of the Kartik's FileInput widget
    5. 'multiple' => true, // If you want to allow multiple upload, default to false
    6. ],
    7. 'pluginOptions' => [ // Plugin options of the Kartik's FileInput widget
    8. 'maxFileCount' => 10 // Client max files
    9. ]
    10. ]) ?>
  2. Use widget to show all attachments of the model in the view.php

    1. <?= \fredyns\attachments\components\AttachmentsTable::widget([
    2. 'model' => $model,
    3. 'showDeleteButton' => false, // Optional. Default value is true
    4. ])?>
  3. You can get all attached files by calling $model->files, for example:

    1. foreach ($model->files as $file) {
    2. echo $file->path;
    3. }

Using Events

You may add the following function to your model

  1. public function init(){
  2. $this->on(\fredyns\attachments\behaviors\FileBehavior::EVENT_AFTER_ATTACH_FILES, function ($event) {
  3. /** @var $files \fredyns\attachments\models\File[] */
  4. $files = $event->files;
  5. //your custom code
  6. });
  7. parent::init();
  8. }