Zend framework ZF呈现一个动作并在另一个动作中获取html
我想用Zend Framework做的是从action X渲染action Y并获得html: 例如:Zend framework ZF呈现一个动作并在另一个动作中获取html,zend-framework,view,frameworks,action,render,Zend Framework,View,Frameworks,Action,Render,我想用Zend Framework做的是从action X渲染action Y并获得html: 例如: public xAction(){ $html = some_function_that_render_action('y'); } public yAction(){ $this->view->somedata = 'sometext'; } 其中y视图类似于: <h1>Y View</h1> <p>Somedata =
public xAction(){
$html = some_function_that_render_action('y');
}
public yAction(){
$this->view->somedata = 'sometext';
}
其中y视图类似于:
<h1>Y View</h1>
<p>Somedata = <?php echo $this->somedata ?></p>
Y视图
某些数据=
我找到了动作助手,但无法从控制器使用它。我怎样才能解决它?
这是可能的吗?这里有一种可能的方法来做你想做的事
public function xAction()
{
$this->_helper
->viewRenderer
->setRender('y'); // render y.phtml viewscript instead of x.phtml
$this->yAction();
// now yAction has been called and zend view will render y.phtml instead of x.phtml
}
public function yAction()
{
// action code here that assigns to the view.
}
您也可以调用yAction,而不是使用来设置要使用的视图脚本,如我上面所示,但是通过调用$html=$this->view->render('controller/y.phtml')来获取html代码>
另请参见。最后我找到了这个“解决方案”,这不是我想要做的,但它是有效的,如果有人找到了真正的解决方案,请在这里回答
public function xAction(){
$data = $this->_prepareData();
$view = new Zend_View();
$view->somedata = $data;
$view->setScriptPath($this->view->getScriptPaths());
$html = $view->render('controller/y.phtml');
}
可以从控制器使用“动作视图”辅助对象
public function xAction()
{
$html = $this->view->action(
'y',
$this->getRequest()->getControllerName(),
null,
$this->getRequest()->getParams()
);
}
public function yAction()
{
// action code here that assigns to the view.
}
它不是很漂亮,但它工作得很好,您不必使用$view->setScriptPath($this->view->getscriptpath())代码>
此帮助程序为yaAction()创建一个新的Zend_控制器请求,因此您可以将自己的参数作为第四个参数,或者使用$This->getRequest()->getParams()
扩展xaAction()的请求参数
采埃孚是否支持“分部”?让xAction和YaAction调用它们的特定模板,并在这些特定模板中调用共享部分模板。您的第一个解决方案无法解决我的问题:它更改了操作的视图,但我只需要html。您的第二个解决方案返回html,但没有为视图分配正确的数据(不要调用操作)。调用new Zend_View()重置视图,必须重新定义在其他文件夹和命名空间中注册的帮助程序