我们接着看CWebApplication的构造函数
public function __construct($config = null)
{
......
//详见下文 configure方法
$this - > configure($config);
//绑定行为
$this->attachBehaviors($this->behaviors);
//预加载组件
$this->preloadComponents();
//详见下文 init()方法
$this->init();
}
configure方法是将config数组遍历赋值给webapp应用,如果webapp属性存在则直接赋值,如果属性私有或者不存在则调用相应的setter方法
public function configure($config)
{
if(is_array($config))
{
foreach($config as $key=>$value)
$this->$key=$value;
}
}
我们来看下默认的config里面都有些什么
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',//基础应用目录
'name'=>'My Web Application',//应用程序名字
// preloading 'log' component
'preload'=>array('log'),//需要预加载的应用组件
//自动导入的类,调用Yii::import导入
'import'=>array(
'application.models.*',
'application.components.*',
),
//模块
'modules'=>array(
// uncomment the following to enable the Gii tool
/*
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'Enter Your Password Here',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('127.0.0.1','::1'),
),
*/
),
//应用组件配置 使用时Yii::app()->comp
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
// uncomment the following to enable URLs in path-format
/*
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
*/
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdrive',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
*/
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
// uncomment the following to show log messages on web pages
/*
array(
'class'=>'CWebLogRoute',
),
*/
),
),
),
//系统参数,使用时 using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster@example.com',
),
);
除了默认的这些配置,我们还可以配置哪些咧,只要是cwebapplication的属性,我们都可以配置
例如:我们可以配置默认的语言和时区
return array(
...
'language'=>'zh_cn',
'timeZone'=>'PRC',
...
);
预加载request组件
protected function init()
{
parent::init();
// preload 'request' so that it has chance to respond to onBeginRequest event.
$this->getRequest();
}
至此实例化webapp应用成功
将webapp对象赋值给Yii的$_app属性----》设置应用基础目录------》设置路径别名-----》预初始化-----》注册异常处理方法---》注册应用核心组件----》初始化配置文件----》绑定应用行为----》预加载组件-----》初始化(预加载request组件)