File or Image upload behavior for Yii2 applications
This module changes the route system definition of Yii2 in order to, instead of having to define the routes in the config file of the application now will be possible to make a series of files that hold the routes that the user will define for his web. This module lets the calling to a series of methods that will define the system routes in a more intuitive way that the basic Yii2 system getting it's inspiration from the routing system defined by Laravel.
Developed by Joseba Juániz (@Patroklo)
This is free software. It is released under the terms of the following BSD License.
Copyright (c) 2015, by Cyneek All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"cyneek/yii2-upload-behavior": "*"
...
'modules' => [
'uploadBehavior' => [
'class' => 'cyneek\yii2\uploadBehavior\Module',
],
]
...
Execute the migration file
php yii migrate --migrationPath=@vendor/cyneek/yii2-upload-behavior/migrations
Profit!
This behavior will take charge of uploading files, while the rules of the form should be stored in the parent model holding this behavior.
attribute (required) The attribute that will link the model with the file we want to upload.
scenarios (default: "default", "insert", "update", "delete") Scenarios in which the upload will take place.
fileActionOnSave (default: insert)(required) Action that will take place when uploading a file. Valid actions:
path The base path or path alias to the directory in which to save files.
instanceByName Getting file instance by name.
multiUpload (default: false) If true, the behavior will check for multiple uploaded files instead of only one.
It has the basic UploadBehavior attributes and a group of image related additional functions:
thumbs Array list with the thumbnail properties and actions
[
'thumbName' => [
['action' => 'crop', 'width' => 200, 'height' => 200, 'quality' => 90],
...
]
]
thumbPath Path where the thumbs will be moved.
imageActions Describes the actions that will be made to the original image
[
['action' => 'crop', 'width' => 200, 'height' => 200, 'quality' => 90]
]
The image actions use the yii\image
class by default.
crop Needs a width (int)
, height (int)
and start (int[x,y])
values. By default start will be [0,0].
thumbnail width (int)
, height (int)
and mode (string)
values. The valid modes are (ManipulatorInterface::THUMBNAIL_INSET
or ManipulatorInterface::THUMBNAIL_OUTBOUND
).
public $file;
public $avatar;
function behaviors()
{
return [
[
'class' => UploadBehavior::className(),
'attribute' => 'file',
'scenarios' => ['default'],
'fileActionOnSave' => 'delete'
],
[
'class' => UploadImageBehavior::className(),
'attribute' => 'avatar',
'scnearios' => ['default'],
'fileActionOnSave' => 'delete'
'imageActions' => [['action' => 'thumbnail', 'width' => '900', 'height' => '400']]
],
];
}
public function rules()
{
return [
...
[['file', 'avatar'], 'file', 'on' => ['insert', 'update', 'default']],
['file', 'required', 'on' => ['insert']],
['avatar', 'file', 'extensions' => ['jpg'], 'maxSize' => 1020*1024]
...
];
}
<?= $form->field($model, 'file')->fileInput() ?>
<?= $form->field($model, 'avatar')->fileInput() ?>
$object = MethodClass::find()->where(['id' => 1])->one();
$file = $object->linkedFile('file');
$object = MethodClass::find()->where(['id' => 1])->one();
$fileList = $object->linkedFiles('file');
$object = MethodClass::find()->where(['id' => 1])->one();
$file = $object->linkedFile('file');
$object->deleteFiles('file', $file);
$object = MethodClass::find()->where(['id' => 1])->one();
$object->deleteFiles('file');
$object = MethodClass::find()->where(['id' => 1])->one();
$file = $object->linkedFile('file');
$thumbnail = $file->getChild('thumb');
$object = MethodClass::find()->where(['id' => 1])->one();
$file = $object->linkedFile('file');
$thumbnailList = $file->getChildren();
Comments