There are two ways to render the view. One is to use action render helper:Zend_Controller_Action_Helper_ViewRenderer, The other way is to use render() method in the Zend_Controller_Action.
You may be curious of the difference between these two approaches. In my eyes, Zend_Controller_Action_Helper_ViewRenderer provides us a more helpful way to render the view.
You may be curious of the difference between these two approaches. In my eyes, Zend_Controller_Action_Helper_ViewRenderer provides us a more helpful way to render the view.
1.The logic in the Zend_Controller_Action's rende() method
public function render($action = null, $name = null, $noController = false)
{
if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
return $this->_helper->viewRenderer->render($action, $name, $noController);
}
$view = $this->initView();
$script = $this->getViewScript($action, $noController);
$this->getResponse()->appendBody(
$view->render($script),
$name
);
}
2.The magic power in Zend_Controller_Action_Helper_ViewRenderer
public function postDispatch()
{
if ($this->_shouldRender()) {
$this->render();
}
} public function render($action = null, $name = null, $noController = null)
{
$this->setRender($action, $name, $noController);
$path = $this->getViewScript();
$this->renderScript($path, $name);
} public function renderScript($script, $name = null)
{
if (null === $name) {
$name = $this->getResponseSegment();
}
$this->getResponse()->appendBody(
$this->view->render($script),
$name
);
$this->setNoRender();
}
本文探讨了在Zend框架中两种不同的视图渲染方法:使用Zend_Controller_Action_Helper_ViewRenderer辅助函数与通过Zend_Controller_Action中的render()方法直接进行渲染。文章详细对比了这两种方式的实现逻辑,并解释了它们之间的差异。
2895

被折叠的 条评论
为什么被折叠?



