【发布时间】:2012-10-05 09:54:04
【问题描述】:
我有两个模块 Admin 和 Login。
我想在管理视图“index.html”中显示登录视图“login.phtml”
我在管理模块 indexAction 控制器中有以下内容
public function indexAction()
{
$login = new LoginController();
$view = new ViewModel(array(
'theloginform' => $login->loginAction(),
));
return $view;
}
在 Login 控制器的 LoginAction 方法中,我返回“login.phtml”文件的 ViewModel。
public function LoginAction() {
$view = new ViewModel();
return $view;
}
indexAction 引发错误,因为变量 'theloginform' 是一个对象。
Catchable fatal error: Object of class Zend\View\Model\ViewModel could not be converted to string in...
如果我添加以下内容:
$authentication->loginAction()->captureTo('test')
“index.phtml”显示字符串“内容”。
我已经读到我可能需要在将 ViewModel 分配给视图变量“theloginform”之前渲染它,但我似乎无法让它工作,我尝试了以下方法但没有运气。
public function LoginAction() {
$view = new ViewModel();
$renderer = new PhpRenderer();
$resolver = new Resolver\AggregateResolver();
$map = new Resolver\TemplateMapResolver(array(
'login' => __DIR__ . '/../view/login.phtml'
));
$resolver->attach($map);
$view->setTemplate("login");
return $renderer->render($view);
}
如果出现以下错误:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "login"; resolver could not resolve to a file
我什至尝试将 DI 添加到 autoload_classmap.php 文件中,但仍然出现相同的错误,我仔细检查了 login.phtml 文件的路径是否正确:
'/Login/view/login/login/login.phtml' 我什至把它复制到了'/Login/src/Login/view/login.phtml'
非常困惑已经阅读然后重新阅读 Zend 文档,我只是想将一个视图传递给另一个视图......
【问题讨论】:
-
为什么要调用另一个视图?你想达到什么目标?
-
在管理模块中,我有“index.phtml”,我想将登录模块“login.phtml”添加到其中。登录模块“login.phtml”视图也将添加到其他模块中的其他视图,例如客户帐户模块。
-
我懒得在午休前回答,但请到 Rob Allens 游乐场看看 > github.com/akrabat/ZF2TestApp/blob/master/module/Application/… 这对你有很大帮助。您可能还对以下问题感兴趣>stackoverflow.com/questions/12451399/…
标签: php zend-framework2