首先搭建demo blog环境,更改数据库连接如下
1 2 3 4 5 6 7 8
| 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=blog', 'emulatePrepare' => true, 'username' => 'root', 'password' => '123456', 'charset' => 'utf8', 'tablePrefix' => 'tbl_', ),
|
从http://www.blog.test/index.php?r=post/index访问demo blog。下面我们来一步步分解yii的执行流程:
1 2 3 4 5
| $yii=dirname(__FILE__).'/../yii/framework/yii.php'; $config=dirname(__FILE__).'/protected/config/main.php';
require_once($yii); Yii::createWebApplication($config)->run();
|
最后一行代码分解为2部分来看:Yii::createWebApplication()和run()。Yii这个类定义在框架目录的根上yii.php。很明显这个类只是对YiiBase.php的一个包装,我们可以在yii.php按自己的需求定制。追着YiiBase.php,首先请大家快速的预览一下该文件。发现从L14-L43有着大量的常量定义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME',microtime(true));
defined('YII_DEBUG') or define('YII_DEBUG',false);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',0);
defined('YII_ENABLE_EXCEPTION_HANDLER') or define('YII_ENABLE_EXCEPTION_HANDLER',true);
defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER',true);
defined('YII_PATH') or define('YII_PATH',dirname(__FILE__));
defined('YII_ZII_PATH') or define('YII_ZII_PATH',YII_PATH.DIRECTORY_SEPARATOR.'zii');
|
接下来,继续追查YiiBase::createWebApplication()
1 2 3 4 5 6 7 8
| public static function createWebApplication($config=null) { return self::createApplication('CWebApplication',$config); } public static function createApplication($class,$config=null) { return new $class($config); }
|
最终产生了一个CWebApplication实例,并调用了父类CApplication的构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public function __construct($config=null) { Yii::setApplication($this); if(is_string($config)) $config=require($config); if(isset($config['basePath'])) { $this->setBasePath($config['basePath']); unset($config['basePath']); } else $this->setBasePath('protected'); Yii::setPathOfAlias('application',$this->getBasePath()); Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME'])); Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions'); $this->preinit(); $this->initSystemHandlers(); $this->registerCoreComponents(); $this->configure($config); $this->attachBehaviors($this->behaviors); $this->preloadComponents(); $this->init(); } protected function init() { parent::init(); $this->getRequest(); } public function getRequest() { return $this->getComponent('request'); }
|
到现在,Yii::createWebapplication执行完毕,接下来执行了父类CApplication::run()方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public function run() { if($this->hasEventHandler('onBeginRequest')) $this->onBeginRequest(new CEvent($this)); $this->processRequest(); if($this->hasEventHandler('onEndRequest')) $this->onEndRequest(new CEvent($this)); } public function processRequest() { if(is_array($this->catchAllRequest) && isset($this->catchAllRequest[0])) { $route=$this->catchAllRequest[0]; foreach(array_splice($this->catchAllRequest,1) as $name=>$value) $_GET[$name]=$value; } else $route=$this->getUrlManager()->parseUrl($this->getRequest()); $this->runController($route); }
|