Zend_Controller_Front->dispatch 注解

本文介绍Zend框架中的调度过程,包括初始化、路由、插件注册、请求与响应对象配置等关键步骤,并详细解释了如何通过Zend_Controller_Front进行调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();    //发送响应到客户端

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值