1.大概过程
application __construct
设置当前运行实例
->获取配置参数
->设置basepath
->preinit
->加载核心组件 registerCoreComponents
->处理加载config中的preload
->初始化init()->加载CHttpRequest组件->run() processRequest()处理请求
->分析路由,创建controller实例执行runController
->runAction->render()渲染视图
2.yii 加载运行流程分析
//单入口文件
require __DIR__ . '/../protected/yii.php'; //包含Yii框架,引入全局类Yii
Yii::createWebApplication($config)->run(); //由配置文件建立应用实例,运行Yii::app()
//yii.php 创建应用
public static function createWebApplication($config=null) {
if ($_SERVER["REQUEST_METHOD"] == "OPTIONS") {
...
} else {
return self::createApplication('CWebApplication',$config);
}
}
public static function createApplication($class,$config=null) {
// ...
//确定当前要加载的模块
$module = self::_getModule($config["modules"]);
//设置module的路径别名
Yii::module($config["name"]);
Yii::setPathOfAlias("module", sprintf("%s/modules/%s/", $rootPath, $module));
//创建CWebApplication
$app = parent::createApplication($class, $config);
return $app;
}
//YiiBase.php
public static function createApplication($class,$config=null)
{
return new $class($config);
}
//执行new CWebApplication()的时候,会将配置信息数组$config传递给CWebApplication创建出对象,并执行对象的run() 方法启动
//CWebApplication类的继承关系
CWebApplication -> CApplication -> CModule -> CComponent
//$config先被传递给CApplication的构造函数
public function __construct($config=null)
//将自身实例赋给Yii的静态成员$_app
Yii::setApplication($this)
//设置了路径别名application和webroot
Yii::setPathOfAlias('application',$this->getBasePath());
Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
//注册核心组件
$this->registerCoreComponents();
//Component通过CModule __get()和__set()方法访问,在访问时由CModule来创建
$this->configure($config);
$this->preloadComponents();
$this->init();
public function run()
//cwebapplication->processRequest //处理请求
$this->processRequest();
//由url得到route找到控制器/创建动作
$route=$this->getUrlManager()->parseUrl($this->getRequest());
$this->runController($route);
public function runController($route)
//创建controller实例,并运行控制器
if(($ca=$this->createController($route))!==null)
//最后执行页面请求
$controller->run($actionID);
application __construct
设置当前运行实例
->获取配置参数
->设置basepath
->preinit
->加载核心组件 registerCoreComponents
->处理加载config中的preload
->初始化init()->加载CHttpRequest组件->run() processRequest()处理请求
->分析路由,创建controller实例执行runController
->runAction->render()渲染视图
2.yii 加载运行流程分析
//单入口文件
require __DIR__ . '/../protected/yii.php'; //包含Yii框架,引入全局类Yii
Yii::createWebApplication($config)->run(); //由配置文件建立应用实例,运行Yii::app()
//yii.php 创建应用
public static function createWebApplication($config=null) {
if ($_SERVER["REQUEST_METHOD"] == "OPTIONS") {
...
} else {
return self::createApplication('CWebApplication',$config);
}
}
public static function createApplication($class,$config=null) {
// ...
//确定当前要加载的模块
$module = self::_getModule($config["modules"]);
//设置module的路径别名
Yii::module($config["name"]);
Yii::setPathOfAlias("module", sprintf("%s/modules/%s/", $rootPath, $module));
//创建CWebApplication
$app = parent::createApplication($class, $config);
return $app;
}
//YiiBase.php
public static function createApplication($class,$config=null)
{
return new $class($config);
}
//执行new CWebApplication()的时候,会将配置信息数组$config传递给CWebApplication创建出对象,并执行对象的run() 方法启动
//CWebApplication类的继承关系
CWebApplication -> CApplication -> CModule -> CComponent
//$config先被传递给CApplication的构造函数
public function __construct($config=null)
//将自身实例赋给Yii的静态成员$_app
Yii::setApplication($this)
//设置了路径别名application和webroot
Yii::setPathOfAlias('application',$this->getBasePath());
Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
//注册核心组件
$this->registerCoreComponents();
//Component通过CModule __get()和__set()方法访问,在访问时由CModule来创建
$this->configure($config);
$this->preloadComponents();
$this->init();
public function run()
//cwebapplication->processRequest //处理请求
$this->processRequest();
//由url得到route找到控制器/创建动作
$route=$this->getUrlManager()->parseUrl($this->getRequest());
$this->runController($route);
public function runController($route)
//创建controller实例,并运行控制器
if(($ca=$this->createController($route))!==null)
//最后执行页面请求
$controller->run($actionID);