fredyns/yii2-attachments

Extension for file uploading and attaching to the models

1.1.1 2021-03-19 17:23 UTC

This package is auto-updated.

Last update: 2024-04-21 15:07:13 UTC


README

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)

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

    Either run

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

    or add

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

    to the require section of your composer.json file.

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

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

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

    	'controllerMap' => [
    	...
    	'migrate' => [
    		'class' => 'yii\console\controllers\MigrateController',
    		'migrationNamespaces' => [
    			'fredyns\attachments\migrations',
    		],
    	],
    	...
    	],
    php yii migrate/up
    
  5. Attach behavior to your model (be sure that your model has "id" property)

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

  7. 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

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

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

    foreach ($model->files as $file) {
        echo $file->path;
    }

Using Events

You may add the following function to your model

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