读书人

Yii分析四:run的执行

发布时间: 2012-10-24 14:15:58 作者: rapoo

Yii分析4:run的执行

在创建完了一个Application之后,就是通过run方法来“运行”这个应用程序了:
$app = Yii::CreateWebApplication($config);?
$app->run();?

下面,我们就来看看,run这个方法是怎么执行的,首先run方法并不是在一个具体的application(比如CWebApplication)中定义的,而是在他们的父类CApplication中定义的,代码如下:

?

public function run(){//触发一个请求开始前的事件        $this->onBeginRequest(new CEvent($this));//处理具体的请求        $this->processRequest();//触发请求完成之后的事件        $this->onEndRequest(new CEvent($this));} public function onBeginRequest($event)                                        {    $this->raiseEvent('onBeginRequest',$event);                               }  

?基于《Yii分析2:组件的事件机制》的分析,我们知道这里其实是在调用一个onBeginRequest的方法,而这个方法从哪里来呢,回到《Yii分析1:web程序入口(2)》,在CreateApplication时,注册了一个组件request,也就是CHttpRequest,这个组件的初始化代码如下:

/**                                                                                * Initializes the application component.                                          * This method overrides the parent implementation by preprocessing                * the user request data.                                                          */                                                                               public function init()                                                            {                                                                                     parent::init();                                                                   $this->normalizeRequest();                                                    }        /**                                                                                * Normalizes the request data.                                                    * This method strips off slashes in request data if get_magic_quotes_gpc() returns true.     * It also performs CSRF validation if {@link enableCsrfValidation} is true.       */    protected function normalizeRequest()                                             {        // normalize request                                                              if(get_magic_quotes_gpc())                                                        {            if(isset($_GET))                                                                      $_GET=$this->stripSlashes($_GET);                                             if(isset($_POST))                $_POST=$this->stripSlashes($_POST);                                           if(isset($_REQUEST))                $_REQUEST=$this->stripSlashes($_REQUEST);            if(isset($_COOKIE))                $_COOKIE=$this->stripSlashes($_COOKIE);        }//enableCsrfValidation初始值为false,因此onBeginRequest并没有在CWebApplication中注册        if($this->enableCsrfValidation)//注册事件处理函数            Yii::app()->attachEventHandler('onBeginRequest',array($this,'validateCsrfToken'));    }

?接下来是核心的“处理请求”的过程了,我们来看一下CWebApplication的代码:

   /** * Processes the current request. * It first resolves the request into controller and action, * and then creates the controller to perform the action. */public function processRequest(){//catchAllRequest用于yiilite,yiilite的使用请查看(http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.performance)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//使用路由管理器来解析url,返回路由的信息$route=$this->getUrlManager()->parseUrl($this->getRequest());//将路由信息作为参数传给runController来运行相应的Controller$this->runController($route);}
?

run的执行就分析到这里,路由管理类和runcontroller我会再分两篇文章进行分析。

读书人网 >编程

热点推荐