Like FIMAk, it is well documented in documents, but still it was not entirely clear how to use the loaded library with the autoloader class. So here is what I did:
1) create a new folder in the vendor directory and place the library there
2) an autoloader is required in the script input before Yii is enabled, in the case of the basic application template, this is path_to_yii_installation/web/index.php
For example, in one of the projects that I worked on before I had a requirement to generate Excel worksheets, I found that PHPExcel is the best library for this. Therefore, I enabled the PHPExcel autoloader as follows:
require(__DIR__ . '/../vendor/excel/PHPExcel.php');
My full index.php file is as follows:
<?php // comment out the following two lines when deployed to production // defined('YII_DEBUG') or define('YII_DEBUG', true); // defined('YII_ENV') or define('YII_ENV', 'dev'); require(__DIR__ . '/../vendor/excel/PHPExcel.php'); require(__DIR__ . '/../vendor/autoload.php'); require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); $config = require(__DIR__ . '/../config/web.php'); (new yii\web\Application($config))->run();
3) at the top of your controller, model, or wherever you want to use the library, add use LibraryClassName , and thatβs all.
source share