Zend_Controller_Front
public function dispatch(Zend_Controller_Request_Abstract $request = null , Zend_Controller_Response_Abstract $response = null )
{
// 注册出错的插件
if ( ! $this -> getParam( ' noErrorHandler ' ) && ! $this -> _plugins -> hasPlugin( ' Zend_Controller_Plugin_ErrorHandler ' )) {
// Register with stack index of 100 该插件存放在序号是 100的位置
$this -> _plugins -> registerPlugin( new Zend_Controller_Plugin_ErrorHandler() , 100 );
}
// 添加视图渲染助手
if ( ! $this -> getParam( ' noViewRenderer ' ) && ! Zend_Controller_Action_HelperBroker :: hasHelper( ' viewRenderer ' )) {
Zend_Controller_Action_HelperBroker :: addHelper( new Zend_Controller_Action_Helper_ViewRenderer());
}
// 设置请求对象 如果没提供则实例化默认http方式的请求对象(该对象封装了 module controller action 对应的名字 及对$_GET $_POST )
/* *
* Instantiate default request object (HTTP version) if none provided
*/
if ( null !== $request ) {
$this -> setRequest( $request );
} elseif (( null === $request ) && ( null === ( $request = $this -> getRequest()))) {
require_once ' Zend/Controller/Request/Http.php ' ;
$request = new Zend_Controller_Request_Http();
$this -> setRequest( $request );
}
// 重新设置 BaseUrl 通过调用 request对象的 setBaseUrl
/* *
* Set base URL of request object, if available
*/
if ( is_callable ( array ( $this -> _request , ' setBaseUrl ' ))) {
if ( null !== ( $baseUrl = $this -> getBaseUrl())) { // 存在 $baseUrl 时才执行
$this -> _request -> setBaseUrl( $baseUrl );
}
}
// 设置 响应对象 如果没提供则实例化默认http方式的响应对象 (该对象处理服务端向客户端的响应 比如 显示网页内容)
/* *
* Instantiate default response object (HTTP version) if none provided
*/
if ( null !== $response ) {
$this -> setResponse( $response );
} elseif (( null === $this -> _response) && ( null === ( $this -> _response = $this -> getResponse()))) {
require_once ' Zend/Controller/Response/Http.php ' ;
$response = new Zend_Controller_Response_Http();
$this -> setResponse( $response );
}
// 把 request 和 response 对象保存在 提供的plugin 中
/* *
* Register request and response objects with plugin broker
*/
$this -> _plugins
-> setRequest( $this -> _request)
-> setResponse( $this -> _response);
// 初始化路由
/* *
* Initialize router
*/
$router = $this -> getRouter(); // 这里是采用 Zend_Controller_Router_Rewrite
$router -> setParams( $this -> getParams()); // 传递引用的参数到路由
//初始化调配器
/* *
* Initialize dispatcher
*/
$dispatcher = $this -> getDispatcher(); // 这里是采用 Zend_Controller_Dispatcher_Standard
$dispatcher -> setParams( $this -> getParams())
-> setResponse( $this -> _response); // 保存 Response 在 dispatcher 因为在 Zend_Controller_Action 中用到
//开始 调配过程
// Begin dispatch
try {
/* *
* Route request to controller/action, if a router is provided
*/
/* *
* Notify plugins of router startup 通知各个插件 路由启动
*/
$this -> _plugins -> routeStartup( $this -> _request);
$router -> route( $this -> _request); // 启动路由 得到了 module controller action 值
/* *
* Notify plugins of router completion 通知各个插件 路由结束
*/
$this -> _plugins -> routeShutdown( $this -> _request);
/* *
* Notify plugins of dispatch loop startup 通知各个插件 调配 循环开始
*/
$this -> _plugins -> dispatchLoopStartup( $this -> _request);
/* *
* Attempt to dispatch the controller/action. If the $this->_request
* indicates that it needs to be dispatched, move to the next
* action in the request.
*/
// 根据当前的 controller/action 尝试进行调配 对于Request 指定 dispatched 的请求 跳过
do {
$this -> _request -> setDispatched( true );
/* *
* Notify plugins of dispatch startup 通知插件 开始调配
*/
$this -> _plugins -> preDispatch( $this -> _request);
/* *
* Skip requested action if preDispatch() has reset it 对于Request 指定 dispatched 的请求 跳过
*/
if ( ! $this -> _request -> isDispatched()) {
continue ;
}
/* *
* Dispatch request 调用 Zend_Controller_Dispatcher_Standard-dispatch()
*/
try {
$dispatcher -> dispatch( $this -> _request , $this -> _response);
} catch ( Exception $e ) {
if ( $this -> throwExceptions()) {
throw $e ;
}
$this -> _response -> setException( $e );
}
/* *
* Notify plugins of dispatch completion 调用 Zend_Controller_Plugin_Broker->postDispatch 通知所有插件完成当前调配
*/
$this -> _plugins -> postDispatch( $this -> _request);
} while ( ! $this -> _request -> isDispatched());
} catch ( Exception $e ) {
if ( $this -> throwExceptions()) {
throw $e ;
}
$this -> _response -> setException( $e );
}
/* *
* Notify plugins of dispatch loop completion 通知所有插件完成所有调配
*/
try {
$this -> _plugins -> dispatchLoopShutdown();
} catch ( Exception $e ) {
if ( $this -> throwExceptions()) {
throw $e ;
}
$this -> _response -> setException( $e );
}
if ( $this -> returnResponse()) { // 如果设制了 返回响应
return $this -> _response;
}
$this -> _response -> sendResponse(); // 发送响应到客户端
}
public function dispatch(Zend_Controller_Request_Abstract $request = null , Zend_Controller_Response_Abstract $response = null )
{
// 注册出错的插件
if ( ! $this -> getParam( ' noErrorHandler ' ) && ! $this -> _plugins -> hasPlugin( ' Zend_Controller_Plugin_ErrorHandler ' )) {
// Register with stack index of 100 该插件存放在序号是 100的位置
$this -> _plugins -> registerPlugin( new Zend_Controller_Plugin_ErrorHandler() , 100 );
}
// 添加视图渲染助手
if ( ! $this -> getParam( ' noViewRenderer ' ) && ! Zend_Controller_Action_HelperBroker :: hasHelper( ' viewRenderer ' )) {
Zend_Controller_Action_HelperBroker :: addHelper( new Zend_Controller_Action_Helper_ViewRenderer());
}
// 设置请求对象 如果没提供则实例化默认http方式的请求对象(该对象封装了 module controller action 对应的名字 及对$_GET $_POST )
/* *
* Instantiate default request object (HTTP version) if none provided
*/
if ( null !== $request ) {
$this -> setRequest( $request );
} elseif (( null === $request ) && ( null === ( $request = $this -> getRequest()))) {
require_once ' Zend/Controller/Request/Http.php ' ;
$request = new Zend_Controller_Request_Http();
$this -> setRequest( $request );
}
// 重新设置 BaseUrl 通过调用 request对象的 setBaseUrl
/* *
* Set base URL of request object, if available
*/
if ( is_callable ( array ( $this -> _request , ' setBaseUrl ' ))) {
if ( null !== ( $baseUrl = $this -> getBaseUrl())) { // 存在 $baseUrl 时才执行
$this -> _request -> setBaseUrl( $baseUrl );
}
}
// 设置 响应对象 如果没提供则实例化默认http方式的响应对象 (该对象处理服务端向客户端的响应 比如 显示网页内容)
/* *
* Instantiate default response object (HTTP version) if none provided
*/
if ( null !== $response ) {
$this -> setResponse( $response );
} elseif (( null === $this -> _response) && ( null === ( $this -> _response = $this -> getResponse()))) {
require_once ' Zend/Controller/Response/Http.php ' ;
$response = new Zend_Controller_Response_Http();
$this -> setResponse( $response );
}
// 把 request 和 response 对象保存在 提供的plugin 中
/* *
* Register request and response objects with plugin broker
*/
$this -> _plugins
-> setRequest( $this -> _request)
-> setResponse( $this -> _response);
// 初始化路由
/* *
* Initialize router
*/
$router = $this -> getRouter(); // 这里是采用 Zend_Controller_Router_Rewrite
$router -> setParams( $this -> getParams()); // 传递引用的参数到路由
//初始化调配器
/* *
* Initialize dispatcher
*/
$dispatcher = $this -> getDispatcher(); // 这里是采用 Zend_Controller_Dispatcher_Standard
$dispatcher -> setParams( $this -> getParams())
-> setResponse( $this -> _response); // 保存 Response 在 dispatcher 因为在 Zend_Controller_Action 中用到
//开始 调配过程
// Begin dispatch
try {
/* *
* Route request to controller/action, if a router is provided
*/
/* *
* Notify plugins of router startup 通知各个插件 路由启动
*/
$this -> _plugins -> routeStartup( $this -> _request);
$router -> route( $this -> _request); // 启动路由 得到了 module controller action 值
/* *
* Notify plugins of router completion 通知各个插件 路由结束
*/
$this -> _plugins -> routeShutdown( $this -> _request);
/* *
* Notify plugins of dispatch loop startup 通知各个插件 调配 循环开始
*/
$this -> _plugins -> dispatchLoopStartup( $this -> _request);
/* *
* Attempt to dispatch the controller/action. If the $this->_request
* indicates that it needs to be dispatched, move to the next
* action in the request.
*/
// 根据当前的 controller/action 尝试进行调配 对于Request 指定 dispatched 的请求 跳过
do {
$this -> _request -> setDispatched( true );
/* *
* Notify plugins of dispatch startup 通知插件 开始调配
*/
$this -> _plugins -> preDispatch( $this -> _request);
/* *
* Skip requested action if preDispatch() has reset it 对于Request 指定 dispatched 的请求 跳过
*/
if ( ! $this -> _request -> isDispatched()) {
continue ;
}
/* *
* Dispatch request 调用 Zend_Controller_Dispatcher_Standard-dispatch()
*/
try {
$dispatcher -> dispatch( $this -> _request , $this -> _response);
} catch ( Exception $e ) {
if ( $this -> throwExceptions()) {
throw $e ;
}
$this -> _response -> setException( $e );
}
/* *
* Notify plugins of dispatch completion 调用 Zend_Controller_Plugin_Broker->postDispatch 通知所有插件完成当前调配
*/
$this -> _plugins -> postDispatch( $this -> _request);
} while ( ! $this -> _request -> isDispatched());
} catch ( Exception $e ) {
if ( $this -> throwExceptions()) {
throw $e ;
}
$this -> _response -> setException( $e );
}
/* *
* Notify plugins of dispatch loop completion 通知所有插件完成所有调配
*/
try {
$this -> _plugins -> dispatchLoopShutdown();
} catch ( Exception $e ) {
if ( $this -> throwExceptions()) {
throw $e ;
}
$this -> _response -> setException( $e );
}
if ( $this -> returnResponse()) { // 如果设制了 返回响应
return $this -> _response;
}
$this -> _response -> sendResponse(); // 发送响应到客户端
}