欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Zend的MVC機制使用分析(二)

 更新時間:2013年05月02日 10:38:31   作者:  
本篇文章介紹了,Zend的MVC機制使用分析,需要的朋友參考下

接著上面的一篇


把代碼貼上來

復(fù)制代碼 代碼如下:

$front = Zend_Controller_Front::getInstance();
Zend_Layout::startMvc(array('layoutPath' => USVN_LAYOUTS_DIR));

$front->setRequest(new Zend_Controller_Request_Http());
$front->throwExceptions(true);
$front->setBaseUrl($config->url->base);

$router = new Zend_Controller_Router_Rewrite();
$routes_config = new USVN_Config_Ini(USVN_ROUTES_CONFIG_FILE, USVN_CONFIG_SECTION);
$router->addConfig($routes_config, 'routes');
$front->setRouter($router);
$front->setControllerDirectory(USVN_CONTROLLERS_DIR);

$front->dispatch();

上一篇把前兩句getInstance和startMvc兩個函數(shù)已經(jīng)讀完了,下面是繼續(xù)分析后面的代碼

setRequest($request) 這里是判斷request是否是繼承自Zend_Controller_Request_Abstract,如果是的話就把front的_request賦值為它。

這里需要了解下什么是Zend_Controller_Request_Abstract,它是所有request抽象出來的抽象類。Zend已經(jīng)提供了兩個實現(xiàn)類,Zend_Controller_Request_Http和Zend_Controller_Request_Simple,一般我們搭建服務(wù)器都是http請求,所以你的項目如果需要重新繼承的話,一般都直接繼承Zend_Controller_Request_Http。

Zend_controller_Request_Http中我們經(jīng)常會使用到的getQuery,getCookie,getRequestUri,getBasePath,getParams,getHeader等這些Http通常的選項都已經(jīng)有了。

繼續(xù)講它的基類Zend_Controller_Request_Abstract,這個類的方法包含:

回到代碼
 

$front->setRequest(new Zend_Controller_Request_Http());這里調(diào)用了Zend_Controller_Request_Http的構(gòu)造函數(shù),構(gòu)造函數(shù)在第一次調(diào)用的時候是$this->setRequestUri();其中的setRequestUri很多都是直接使用$_SERVER這個php全局變量中的數(shù)據(jù)來獲取requestUri的。

setRequestUri可以學(xué)到的是在不同的服務(wù)器中如何獲取requestUri(特別是在IIS中的$SERVER中不同的變量組合有不同的含義),比如http://172.23.11.160/usvn/item/usvn_test 這個url,它的requestUri就是/usvn/item/usvn_test

 

$front->throwExceptions(true); 將內(nèi)部的_throwExceptions標(biāo)志位設(shè)置為true;

$front->setbaseUrl("/usvn")這個做了兩件事情,首先是設(shè)置front內(nèi)部的_baseUrl屬性,其次調(diào)用Request的setBaseUrl,也是設(shè)置Zend_Controller_Request_Http的內(nèi)部_baseUrl屬性。


$router = new Zend_Controller_Router_Rewrite();

$routes_config = new USVN_Config_Ini(USVN_ROUTES_CONFIG_FILE, USVN_CONFIG_SECTION);

$router->addConfig($routes_config, 'routes');

$front->setRouter($router);

下面這三行就直接說,實際上就是使用Zend的Router模塊使用配置文件,router使用setRouter放入front里面。


最后一句


$front->dispatch();

這個函數(shù)也是最核心的一個函數(shù)。

這個函數(shù)首先注冊了一個插件Zend_Controller_Plugin_ErrorHandler,index為100,把插件的順序放在最后。

 

第二步存放了一個Helper,Zend_Controller_Action_Helper_ViewRenderer,index為-80

下面實例化了request,request是一個Zend_Controller_Request_Http類型。并將request的baseUrl設(shè)置為前面設(shè)置過的_baseUrl,就是"/usvn/item/usvn_test"

接著實例化了response,response是一個Zend_Controller_Response_Http();

下面使用plugins來對Request和Response進行設(shè)置,首先實際調(diào)用了Zend_Controller_Plugin_Broker的setRequest函數(shù),這個函數(shù)循環(huán)遍歷broker管理的所有插件,調(diào)用插件的setRequest($request)函數(shù)(如果有的話)。

 

接下來初始化router,和設(shè)置router的參數(shù)。router已經(jīng)在前面設(shè)置過了,就是Zend_Controller_Router_Rewrite類型

初始化分發(fā)器dispatcher,分發(fā)器我們是第一次看到,Zend_Controller_Dispatcher_Standard類。分發(fā)器以后再說。


下面的流程:

調(diào)用插件的routeStartup對request進行處理

調(diào)用router的route處理request

調(diào)用插件的routeShutdown對request進行處理

調(diào)用插件的dispatchLoopStartup對request進行處理

進入循環(huán)分發(fā)過程

調(diào)用插件的preDispatch對request進行處理

調(diào)用dispatcher的dispatch處理request和response

調(diào)用插件的postDispatch對request進行處理

跳出循環(huán)分發(fā)過程

調(diào)用插件的dispatchLoopShutdown對request進行處理

發(fā)送response

相關(guān)文章

最新評論