|
| 1 | +# Yii2 upload file behavior # |
| 2 | + |
| 3 | +## About |
| 4 | +Yii2 upload file behavior - simple wey to upload images and files to server. |
| 5 | +No need anymore wrote tonn of code in controller and else testing it by houers. As a result - saving time and labor costs for uploading files to the site. |
| 6 | +Only needed upload extention from github and past some less code to model class (\yii\db\ActiveRecord) where needed hendler uploading files. |
| 7 | +More on this below. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). |
| 12 | + |
| 13 | +First download extention. Run the command in the terminal: |
| 14 | +``` |
| 15 | +composer require "coderius/yii2-upload-file-behavior" |
| 16 | +``` |
| 17 | + |
| 18 | +or add in composer.json |
| 19 | +``` |
| 20 | +"coderius/yii2-upload-file-behavior": "^1.0" |
| 21 | +``` |
| 22 | +and run `composer update` |
| 23 | + |
| 24 | +## Usage |
| 25 | +This extention created for usage in \yii\db\ActiveRecord model classes. |
| 26 | + |
| 27 | +Configyration behavior. |
| 28 | + |
| 29 | +* __$nameOfAttributeFile__ = (string) default name 'file'. Virtual attribute for uploading file instance from file systrem. |
| 30 | +* __$nameOfAttributeStorage__ = (string) default name 'face_img'. Attribute for saving path to uploaded file in db. |
| 31 | +* __$newFileName__ = (string) name which is assigned to uploaded file |
| 32 | +* __$directories__ = (array) configs to upload folder and upload hendlers.Сonsists of separate arrays. |
| 33 | + Each array contains settings for the path to the target folder and a handler for uploading files to this folder like 'path' and 'handler' |
| 34 | + __-'path'__ - contains path to target folder |
| 35 | + __-'hendler'__ - Processes the downloaded file and saves to the specified in param 'path' location. |
| 36 | + |
| 37 | +This extention created for usage in \yii\db\ActiveRecord model classes. |
| 38 | + |
| 39 | +- So, first in model class put namespace to yii2-upload-file-behavior. |
| 40 | +- Create public variable $file for loading file from filesystem. |
| 41 | +- The database must have an attribute to store the file path. In example below it is 'img_src' attribute (marked like save in public function rules()) |
| 42 | +- Then past needed configs behaviors() method like in example. |
| 43 | + |
| 44 | +__!Note.__ _Don't forget to include the dependency namespaces._ |
| 45 | + |
| 46 | +**Example** |
| 47 | + |
| 48 | +``` |
| 49 | + namespase your/models; |
| 50 | +
|
| 51 | + use coderius\yii2UploadFileBehavior\UploadFileBehavior; |
| 52 | + use yii\imagine\Image; |
| 53 | + use Imagine\Image\Point; |
| 54 | + use Imagine\Image\Box; |
| 55 | +
|
| 56 | + class YourModel extends \yii\db\ActiveRecord |
| 57 | + { |
| 58 | + public $file; |
| 59 | +
|
| 60 | + //'img_src' - attribute to save path to file in db |
| 61 | + public function rules() |
| 62 | + { |
| 63 | + return [ |
| 64 | + [['img_src'], 'safe'], |
| 65 | + } |
| 66 | +
|
| 67 | + ... |
| 68 | +
|
| 69 | + public function behaviors() |
| 70 | + { |
| 71 | + return [ |
| 72 | + //Another behaviors |
| 73 | + //... |
| 74 | +
|
| 75 | + 'uploadFileBehavior' => [ |
| 76 | + 'class' => UploadFileBehavior::className(), |
| 77 | + 'nameOfAttributeStorage' => 'img_src', |
| 78 | + 'directories' => [ |
| 79 | + |
| 80 | + [ |
| 81 | + 'path' => function($attributes){ |
| 82 | + return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/big/'); |
| 83 | + }, |
| 84 | + 'hendler' => function($fileTempName, $newFilePath){ |
| 85 | + Image::thumbnail($fileTempName, 900, 900*2/3) |
| 86 | + ->copy() |
| 87 | + ->crop(new Point(0, 0), new Box(900, 900*2/3)) |
| 88 | + ->save($newFilePath, ['quality' => 80]); |
| 89 | + sleep(1); |
| 90 | + } |
| 91 | + ], |
| 92 | + [ |
| 93 | + 'path' => function($attributes){ |
| 94 | + return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/middle/'); |
| 95 | + }, |
| 96 | + 'hendler' => function($fileTempName, $newFilePath){ |
| 97 | + Image::thumbnail($fileTempName, 400, 400*2/3) |
| 98 | + ->save($newFilePath, ['quality' => 80]); |
| 99 | + sleep(1); |
| 100 | + } |
| 101 | + ], |
| 102 | + [ |
| 103 | + 'path' => function($attributes){ |
| 104 | + return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/small/'); |
| 105 | + }, |
| 106 | + 'hendler' => function($fileTempName, $newFilePath){ |
| 107 | + Image::thumbnail($fileTempName, 150, 150*2/3) |
| 108 | + ->save($newFilePath, ['quality' => 80]); |
| 109 | + sleep(1); |
| 110 | + } |
| 111 | + ], |
| 112 | + ] |
| 113 | + ], |
| 114 | +
|
| 115 | + ]; |
| 116 | + } |
| 117 | +
|
| 118 | + ... |
| 119 | + } |
| 120 | +
|
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | +Additional actions: |
| 125 | +------------------------------------- |
| 126 | +1. Create aliases to target folders for saving uploaded files. |
| 127 | +2. Create target folders in 'frontend/web' dirrectory like example. |
| 128 | +3. Don't forget create vertual attribute. If it named like '$file', then no need set config to 'nameOfAttributeFile'(default = (string)'file'). |
| 129 | + |
| 130 | +## Testing |
| 131 | + |
| 132 | +```bash |
| 133 | +$ ./vendor/bin/phpunit |
| 134 | +``` |
| 135 | + |
| 136 | +## Credits |
| 137 | + |
| 138 | +- [Sergio Coderius](https://github.com/coderius) |
| 139 | + |
| 140 | +## License |
| 141 | + |
| 142 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments