Yii配置
yii的配置文件是main.php
基本配置
根据做作者的注释
CWebApplication properties can be configured here.
也就是说配置的是 CWebApplication类的属性都可以配置
/ This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', //基础目录
'name'=>'My Web Application', //应用名称
'preload'=>array('log'), //预加载
'defaultController'=>'Index', //默认控制器
'timeZone'=>'PRC', //时区
'import'=>array( //导入
'application.models.*',
'application.components.*',
),
'modules'=>array( //模块
),
'components'=>array( //应用组件
),
// using Yii::app()->params['paramName']
'params'=>array( //参数 全局配置的参数可以在这里
// this is used in contact page
'adminEmail'=>'webmaster@example.com',
),
);
用法
Yii::app()->params['paramName'];//调用的CComponent中的__get魔术方法
实现原理
继承 CWebApplication » CApplication » CModule » CComponent
1.在项目路口文件 index.php中导入的config数组
$config=dirname(__FILE__).'/protected/config/main.php';
并且传入$config数组,实例化了 CWebApplication类
Yii::createWebApplication($config)->run();
2.调用CApplication类的构造函数
//上面做了的一些 设置路径别名的工作
//并且 删除了 config数组中的 basePath,extensionPath,aliases
$this->configure($config);
public function configure($config)
{
if(is_array($config))
{
foreach($config as $key=>$value)
$this->$key=$value;//调用的 CComponent 的 __set 魔术方法
}
}