Yii2框架配置文件(Application屬性)與調(diào)試技巧實(shí)例分析
本文實(shí)例講述了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;
最后返回的一個(gè)數(shù)組,數(shù)組的key都是Application的屬性。
我們到控制器中來(lái)訪問(wèn)一下:
public function actionIndex() { echo \Yii::$app->id,'<br>'; echo \Yii::$app->name,'<br>'; exit; return $this->render('index',['username'=>'張三','age'=>22]); }
在入口文件web/index.php 里會(huì)加載這個(gè)config.php 配置文件,來(lái)創(chuàng)建一個(gè)Application
#... $config = require(__DIR__ . '/../config/web.php'); (new yii\web\Application($config))->run();
調(diào)試技巧
助手類Yii,服務(wù)于整個(gè)框架,提供一些基礎(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è)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- Yii2學(xué)習(xí)筆記之漢化yii設(shè)置表單的描述(屬性標(biāo)簽attributeLabels)
- 詳解PHP的Yii框架中組件行為的屬性注入和方法注入
- 深入講解PHP的Yii框架中的屬性(Property)
- Yii2中設(shè)置與獲取別名的函數(shù)(setAlias和getAlias)用法分析
- Yii2設(shè)置默認(rèn)控制器的兩種方法
- Yii獲取當(dāng)前url和域名的方法
- Yii2使用$this->context獲取當(dāng)前的Module、Controller(控制器)、Action等
- Yii操作數(shù)據(jù)庫(kù)實(shí)現(xiàn)動(dòng)態(tài)獲取表名的方法
- Yii Framework框架獲取分類下面的所有子類方法
- Yii框架getter與setter方法功能與用法分析
相關(guān)文章
PHP strcmp()和strcasecmp()的區(qū)別實(shí)例
這篇文章主要介紹了PHP strcmp()和strcasecmp()的區(qū)別實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-11-11php文件服務(wù)實(shí)現(xiàn)虛擬掛載其他目錄示例
這篇文章主要介紹了php文件服務(wù)實(shí)現(xiàn)虛擬掛載其他目錄示例,需要的朋友可以參考下2014-04-04php遞歸函數(shù)三種實(shí)現(xiàn)方法及如何實(shí)現(xiàn)數(shù)字累加
實(shí)現(xiàn)遞歸函數(shù)有哪些方法呢?如何用遞歸函數(shù)實(shí)現(xiàn)數(shù)字累加?這篇文章就主要介紹php遞歸函數(shù)三種實(shí)現(xiàn)方法及如何實(shí)現(xiàn)數(shù)字累加,需要的朋友可以參考下。2015-08-08Yii統(tǒng)計(jì)不同類型郵箱數(shù)量的方法
這篇文章主要介紹了Yii統(tǒng)計(jì)不同類型郵箱數(shù)量的方法,涉及Yii數(shù)據(jù)庫(kù)查詢及字符串的遍歷、截取與判斷相關(guān)操作技巧,需要的朋友可以參考下2016-10-10Yii使用CLinkPager分頁(yè)實(shí)例詳解
這篇文章主要介紹了Yii使用CLinkPager分頁(yè)的方法,需要的朋友可以參考下2014-07-07Laravel實(shí)現(xiàn)構(gòu)造函數(shù)自動(dòng)依賴注入的方法
這篇文章主要介紹了Laravel實(shí)現(xiàn)構(gòu)造函數(shù)自動(dòng)依賴注入的方法,涉及Laravel構(gòu)造函數(shù)自動(dòng)初始化的相關(guān)技巧,需要的朋友可以參考下2016-03-03PHP 計(jì)算兩個(gè)特別大的整數(shù)實(shí)例代碼
這篇文章主要介紹了PHP 計(jì)算兩個(gè)特別大的整數(shù)實(shí)例代碼,需要的朋友可以參考下2018-05-05