本文實例講述了Yii2框架配置文件(Application屬性)與調試技巧。分享給大家供大家參考,具體如下:
配置文件
Yii2的主要配置文件config/web.php:
<?php$params = require(__DIR__ . '/params.php');$config = [ 'id' => 'basic', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'components' => [ 'request' => [ // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation 'cookieValidationKey' => 'aldjaldjaldjaljd', ], 'cache' => [ 'class' => 'yii/caching/FileCache', ], 'user' => [ 'identityClass' => 'app/models/User', 'enableAutoLogin' => true, ], 'errorHandler' => [ 'errorAction' => 'site/error', ], 'mailer' => [ 'class' => 'yii/swiftmailer/Mailer', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => true, ], 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii/log/FileTarget', 'levels' => ['error', 'warning'], ], ], ], 'db' => require(__DIR__ . '/db.php'), 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ ], ], ], 'params' => $params,];if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = [ 'class' => 'yii/debug/Module', ]; $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii/gii/Module', ];}return $config;最后返回的一個數組,數組的key都是Application的屬性。
我們到控制器中來訪問一下:
public function actionIndex(){ echo /Yii::$app->id,'<br>'; echo /Yii::$app->name,'<br>'; exit; return $this->render('index',['username'=>'張三','age'=>22]);}
在入口文件web/index.php 里會加載這個config.php 配置文件,來創建一個Application
#...$config = require(__DIR__ . '/../config/web.php');(new yii/web/Application($config))->run();
調試技巧
助手類Yii,服務于整個框架,提供一些基礎方法:記錄日志、調試等
/Yii:warning()日志文件runtime/logs/app.log
/Yii::error()
/Yii::info()
/Yii:trace('調試內容','test')
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選