File Manager
File Manager, (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist jakharbek/yii2-filemanager "*"
or add, (*4)
"jakharbek/yii2-filemanager": "*"
to the require section of your composer.json
file., (*5)
Usage
Once the extension is installed, simply use it in your code by :, (*6)
Migrations
You need to do the migration, (*7)
yii migrate --migrationPath=@vendor/jakharbek/yii2-filemanager/src/migrations
Module
You need to connect the module of the backend part app.\jakharbek\filemanager\backend\Module
, (*8)
'modules' => [
'files' => \jakharbek\filemanager\backend\Module::class
],
Params
You need to add parameters to the application in the file.
example
```php
[
'thumbs' => [
'icon' => [
'w' => 50,
'h' => 50,
'q' => 50,
'slug' => 'icon'
],
'small' => [
'w' => 320,
'h' => 320,
'q' => 50,
'slug' => 'small'
],
'low' => [
'w' => 640,
'h' => 640,
'q' => 50,
'slug' => 'low'
],
'normal' => [
'w' => 1024,
'h' => 1024,
'q' => 50,
'slug' => 'normal'
]
],
'images_ext' => [
'jpg',
'png',
'bmp',
'gif'
],
'use_file_name' => true,
'use_queue' => false,
'file_not_founded' => '14',
//'file_not_founded' => 'http://img.domain.loc/files/1.jpg', (*9)
```
thumbs
- thumbnails images
images_ext
- images ext
use_file_name
- When uploading a file, whether to use the file name in the file download or create a hash
use_queue
- When uploading a file, is it necessary to use a queue to load some photos in the background mode?, (*10)
Apply DI (dependency injection), (*11)
There is a class \jakharbek\filemanager\bootstrap\SetUp
you need to apply it to the initial download of the application as an example., (*12)
'bootstrap' => [
\jakharbek\filemanager\bootstrap\SetUp::class
],
Ways to use, (*13)
There are two ways to use you can use using a relation in a database or a column in a table:, (*14)
Method use via the relation, (*15)
Suppose you have a junction table for example
```
post_image, (*16)
post_id
file_id
sort, (*17)
```, (*18)
And let's say you have the appropriate relational methods in Active Record, (*19)
```php
public function getPostImages()
{
return $this->hasMany(PostImage::className(), ['post_id' => 'post_id']);
}, (*20)
public function getImages()
{
return $this->hasMany(Files::className(), ['file_id' => 'file_id'])->viaTable('postImages', ['post_id' => 'post_id']);
}
```
And now you need to apply special behavior jakharbek\filemanager\behaviors\FileRelationBehavior
for such cases., (*21)
For example:, (*22)
php
'file_relation_image' => [
'class' => FileRelationBehavior::class,
'delimtr' => ',',
'attribute' => 'file_image'
],
, (*23)
You will need to create a property for exchanging data between the form and the model in case it found file_image
, (*24)
php
public $file_image
, (*25)
or, (*26)
```php
private $_file_image;, (*27)
public function getFileImage(){
return $this->$_file_image;
}, (*28)
public function setFileImage($value){
return $this->$_file_image = $value;
}
Next, you need to add this property `file_image` to the rules of the model as `safe`.
For example:
php
public function rules()
{
return [
[['file_image'], 'safe']],
];
}
```
Now you can apply a file load/upload widget jakharbek\filemanager\widgets\FileInput
for this field., (*29)
For example, (*30)
php
<?php use \jakharbek\filemanager\widgets\FileInput;?>
<?= $form->field($model, 'file_image')->widget(FileInput::class,[
'id' => 'file_image_id'
]) ?>
, (*31)
Use without a junction table
I assume that you already have fields in the table for storing file identifiers there. The type of this column should be a string, since file identifiers will be stored there through a separator for example we have images column for posts table, (*32)
In this case, you can immediately use the widget without any model settings., (*33)
For example:, (*34)
use \jakharbek\filemanager\widgets\FileInput;
echo FileInput::widget(['id' => 'post_images_id']);
Use in CKEditor
To use the plug-in file manager in the editor, you need to specify this plug-in in the settings as shown in the example.
For example:, (*35)
<?php
echo $form->field($model, 'description')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'clientOptions'=>[
'extraPlugins' => 'filemanager-jakhar',
'justifyClasses'=>[ 'AlignLeft', 'AlignCenter', 'AlignRight', 'AlignJustify' ],
'height'=>200,
'toolbarGroups' => [
['name' => 'filemanager-jakhar']
],
],
'preset' => 'short'
]);
?>
After you need to run once this code to launch a modal window when you click a button in the editor, (*36)
<?=\jakharbek\filemanager\widgets\FileInput::widget(['id' => 'fileManagerEditor','editor' => true]);?>
Helpers
You have to help the class jakharbek\filemanager\helpers\FileManagerHelper
There is one method that you will often use in my opinion:FileManagerHelper::getFilesById
, (*37)
API
The extension has an API, you can connect it and get access to the file manager via the API
jakharbek\filemanager\api\FilesController
If you need an action of this controller, you can use them by looking at the action method of this class., (*38)