我使用以下代码在 Controller 操作中呈现页面。
public function userinforeceiveAction()
{
$renderer = new PhpRenderer();
$map = new Resolver\TemplateMapResolver(array(
'userinfo' => __DIR__ . '/userinfo.phtml',
));
$resolver = new Resolver\TemplateMapResolver($map);
$renderer->setResolver($resolver);
$model = new ViewModel();
$model->setTemplate('userinfo');
return new ViewModel();
}
我在 View 中回显渲染内容。
echo $renderer->render($model);
但它什么也不渲染。请帮我。谢谢。
以及通过这两行与 zf1 的良好工作。
$this->userinfoAction();
$this->$render('userinfo');
最佳答案
实际上,您所做的应该适用于以下矿工更改
public function userinforeceiveAction()
{
$renderer = new PhpRenderer();
$map = new Resolver\TemplateMapResolver(array(
// first mistake
// __DIR__ is the directory of application controller, not the path of views
// second mistake
// 'userinfo' should be '(controller)/(action)'
'userinfo' => __DIR__ . '/userinfo.phtml',
));
// third mistake
// since $map is a resolver instance, this is wrong
$resolver = new Resolver\TemplateMapResolver($map); // no need
$renderer->setResolver($resolver);
// should be $renderer->setResolver($map);
$model = new ViewModel();
// 'userinfo' should be changed
$model->setTemplate('userinfo');
// big mistake
// what you are returning here is new instance of view model
return new ViewModel();
// instead you should return $model
}
View 中不需要此行
echo $renderer->render($model);
但最好和推荐的方法是通过模块配置文件注入(inject),
正如 Adam 所说
documentation 将提供更好的解释
关于php - 如何在 Zend 框架 2 中呈现页面?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10426967/