ActionDispatcher负责选择正确的显示资源并生成响应页面返回给用户。下图显示了ActionDispatcher的处理过程。

扩展ActionDispatcher
在大部分应用中我们需要扩展框架的ActionDispatcher类来生成自己的分配器,在本例中我们创建的分配器叫做ReportActionDispatcher,在该类中,我们重写serviceResponse()方法,如下:
class ReportActionDispatcher extends ActionDispatcher {
function ReportActionDispatcher($uri='', $wrapper='', $servletPath='',
$pathInfo='', $queryString='', $name='') {
parent::ActionDispatcher($uri='', $wrapper='', $servletPath='',
$pathInfo='', $queryString='', $name='');
$this->log->setLog('isDebugEnabled' , False);
$this->log->setLog('isTraceEnabled' , False);
}
function serviceResponse(&$request, &$response) {
$trace = $this->log->getLog('isTraceEnabled');
if($trace)
$this->log->trace('Start: TestActionDispatcher->serviceResponse(..)['.__LINE__.']');
$requestURI = $this->uri;
$form = $request->getAttribute('ACTION_FORM');
$data = $request->getAttribute('FORM_DATA');
$errors = $request->getAttribute( Action::getKey('ERROR_KEY') );
$pageBuff = '';
ob_start();
include $requestURI;
$pageBuff = ob_get_contents();
ob_end_clean();
$response->setResponseBuffer($pageBuff);
}
}
serviceResponse()方法
在serviceResponse()方法中,我们可以设置是否记录日志。
然后我们得到模板资源的URI
$requestURI = $this->uri;
在本例中URI有salesReportIndex.tpl和salesReport.tpl两个模板文件。
接下来我们得到ActionForm,form data和ActionErrors对象
$form = $request->getAttribute('ACTION_FORM');
$data = $request->getAttribute('FORM_DATA');
$errors = $request->getAttribute( Action::getKey('ERROR_KEY') );
这些对象我们都是在ActionForm和Action类中创建的。
注意:这些对象在这个方法范围内都可以使用,因此在模板中我们也可以使用。
现在我们能在ob_start()和ob_end_clear()之间包含模板资源($requestURI)。
ob_start();
include $requestURI;
$pageBuff = ob_get_contents();
ob_end_clean();
当一个模板文件被包含,在里面的任何PHP子句都会被当作一般的PHP脚本来执行。这样,$form,$data和$errors等对象在模板文件中都是可见的(即可以使用)。
PHP输出缓冲区允许我们获取已经包含的资源并保存到缓冲变量$pageBuff中,$pageBuff变量现在包含了已经完成的HTTP响应内容,就象用户在他/的浏览器上看到的网页一样。
最后我们返回$pageBuff的内容到分配器:
$response->setResponseBuffer($pageBuff);
分配器然后再将$pageBuff的内容作为HTTP响应发送到客户端。
发表于 @ 2004年12月31日 08:33:00|评论(loading...)|编辑