Home >Backend Development >PHP Tutorial >Yii2.0 integrates elfinder picture manager calling method
Yii2 extension: https://github.com/MihailDev/yii2-elfinder
Although the tutorial is in Russian, don’t be afraid, just follow the code
Steps:
1. Use composer to install the extension
composer require --prefer-dist mihaildev/ yii2-elfinder "*"
2. Add configuration information. I am Yii2 advanced template, used in the background, so open the backend/config/main.php configuration file
Add it at the same level as 'components'
'controllerMap' => [ 'elfinder' => [ 'class' => 'mihaildev\elfinder\PathController', 'access' => ['@'], 'root' => [ 'path' => 'uploads', //主目录路径 'name' => 'Files' ],/* 'watermark' => [ 'source' => __DIR__.'/logo.png', // Path to Water mark image 'marginRight' => 5, // Margin right pixel 'marginBottom' => 5, // Margin bottom pixel 'quality' => 95, // JPEG image save quality 'transparency' => 70, // Water mark image transparency ( other than PNG ) 'targetType' => IMG_GIF|IMG_JPG|IMG_PNG|IMG_WBMP, // Target image formats ( bit-field ) 'targetMinPixel' => 200 // Target image minimum pixel size ]*/ ] ],
3. Add Default parameters
Open commmon(backend)/config/params.php
// elfinder默认配置 'elfinderDefaultConfig' => [ 'language' => 'zh_CN', 'controller' => 'elfinder', // 默认控制名 'filter' => 'image', // 只管理图片 https://github.com/Studio-42/elFinder/wiki/Client-configuration-options#wiki-onlyMimes 'template' => '<div class="input-group">{input} <span class="input-group-btn">{button}</span></div>', 'options' => ['class' => 'form-control'], 'buttonOptions' => ['class' => 'btn btn-default'], 'buttonName' => '浏览' ],
4. The last step is to introduce the call
Open the form view file to be used and add the following code
use mihaildev\elfinder\InputFile; <?php $path = 'carLogo'; // 存放目录,改为实际的,elfinder会自动创建 $elfinderConfig = array_merge(Yii::$app->params['elfinderDefaultConfig'],['path' => $dir]); echo $form->field($model, 'carLogo')->widget(InputFile::className(),$elfinderConfig); ?>
Final effect: