本文實例講述了Yii2框架配置文件(Application屬性)與調(diào)試技巧。分享給大家供大家參考,具體如下:
配置文件
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;
最后返回的一個數(shù)組,數(shù)組的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 配置文件,來創(chuàng)建一個Application
#... $config = require(__DIR__ . '/../config/web.php'); (new yii\web\Application($config))->run();
調(diào)試技巧
助手類Yii,服務(wù)于整個框架,提供一些基礎(chǔ)方法:記錄日志、調(diào)試等
\Yii:warning()
日志文件runtime/logs/app.log
\Yii::error()
\Yii::info()
\Yii:trace('調(diào)試內(nèi)容','test')
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計有所幫助。