Archive for the ‘yii’ Category

Yii can call controller action from another controller

Yii framework has runController method https://www.yiiframework.com/doc/api/1.1/CWebApplication#runController-detail It is additional feature which classic MVC frameworks dont have

Yii2 print ActiveRecord query sql output

<?php   echo $query->createCommand()->sql;

Yii 2 Download a File

<?php   return Yii::$app->response->sendFile($fileName);

Yii2 custom logger

Step 1: app/config/console.php   ‘log’ => [ ‘traceLevel’ => YII_DEBUG ? 3 : 0, ‘targets’ => [ [ ‘class’ => ‘app\helpers\MyFileTarget’, ‘exportInterval’ => 1, ‘categories’ => [’my-category’], ‘except’ => [’application’], ‘levels’ => [’trace’], ‘logFile’ => ‘@app/runtime/logs/my-category.log’, ], ], ], Step 2: Create file app/helpers/MyFileTarget.php   <?php   namespace app\helpers;   use Yii; use yii\log\Logger; use […]

Yii routing SEO friendly route

To make the following url work: http://www.website.com/green-juice add the following rule to config/web.php ‘urlManager’ => [ ‘enablePrettyUrl’ => true, ‘showScriptName’ => false, ‘rules’ => [ ‘green-juice’=>’green/say’, ] ]

Yii routing enable human readable uri

Edit file config/web.php Add the following to the ‘components’ array: ‘components’ => [ ‘urlManager’ => [ ‘enablePrettyUrl’ => true, ‘showScriptName’ => false, ]

Yii turn debug panel on/off

Edit config/main.php (or config/web.php), add or remove the following code: $config[‘bootstrap’][] = ‘debug’; $config[‘modules’][‘debug’] = ‘yii\debug\Module’;