我有兩個模塊Admin和Login。Zend Framework 2在視圖中顯示視圖
欲管理員視圖「index.html的」
在管理模塊的indexAction控制器
public function indexAction()
{
$login = new LoginController();
$view = new ViewModel(array(
'theloginform' => $login->loginAction(),
));
return $view;
}
我有以下在的LoginAction方法中顯示登錄視圖「login.phtml」在登錄控制器中,我返回'login.phtml'文件的ViewModel。
public function LoginAction() {
$view = new ViewModel();
return $view;
}
由於變量'theloginform'是一個對象,indexAction會拋出一個錯誤。
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 d ocumentation,我只是想一個視圖傳遞到另一個視圖...
爲什麼你想調用另一個視圖?你想實現什麼? –
在管理模塊中,我有'index.phtml',我想添加登錄模塊'login.phtml'。登錄模塊'login.phtml'視圖還將在其他模塊(如客戶帳戶模塊)中添加其他視圖。 –
我懶得在午餐休息之前回答問題,但請查看Rob Allens playground> https://github.com/akrabat/ZF2TestApp/blob/master/module/Application/src/Application/Controller/ViewController.php它會幫助你很多。您可能也有興趣在下面的問題> http://stackoverflow.com/questions/12451399/how-to-render-zf2-view-within-json-response/12651250#12651250 – Sam