To integrate Filepicker with Yii 2 follow these steps:
Copy the assets
folder to your Yii web
directory and the uploader
folder into the root directory.
Edit your composer.json
file and add the following line to the psr-4
autoload:
"Hazzard\\Filepicker\\": "app/uploader/src"
It should look like this.
Then run:
composer require hazzard/config
composer require intervention/image
composer require symfony/http-foundation ^2.7
composer dumpautoload
Create a FilepickerController
controller and add:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use Hazzard\Filepicker\Handler;
use Hazzard\Filepicker\Uploader;
use Intervention\Image\ImageManager;
use Hazzard\Config\Repository as Config;
class FilepickerController extends Controller
{
public function actionHandle()
{
$handler = new Handler(
new Uploader($config = new Config, new ImageManager)
);
$config['debug'] = true;
// Path to the files directory (web/files).
$config['upload_dir'] = Yii::getAlias('@webroot') . '/files';
// Url to the files directory.
$config['upload_url'] = Yii::getAlias('@web') . '/files';
$handler->handle()->send();
}
}
Then in your view make sure to set the url option to your controller route, like this:
url: '<?php echo yii\helpers\Url::to(["filepicker/handle"]); ?>',
Add add the CSRF token using the data option:
data: {
_csrf: '<?php echo Yii::$app->request->getCsrfToken(); ?>',
}