Zend Framework 2 在视图中显示视图 [英] Zend Framework 2 display a view within a view

查看:25
本文介绍了Zend Framework 2 在视图中显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模块管理和登录.

I have two modules Admin and Login.

我想在管理视图index.html"中显示登录视图login.phtml"

I want to display the Login view 'login.phtml' within the admin view 'index.html'

我在管理模块 indexAction 控制器中有以下内容

I have the following in the Admin modules indexAction controller

public function indexAction()
{    
    $login = new LoginController();

    $view = new ViewModel(array(
        'theloginform' => $login->loginAction(),
    ));

    return $view;
}

在登录控制器的 LoginAction 方法中,我返回login.phtml"文件的 ViewModel.

In the LoginAction method in the Login controller I return the ViewModel for the 'login.phtml' file.

public function LoginAction() {
       $view = new ViewModel();
       return $view;
}

indexAction 抛出错误,因为变量theloginform"是一个对象.

The indexAction throws an error as the variable 'theloginform' is an object.

Catchable fatal error: Object of class Zend\View\Model\ViewModel could not be converted to string in...

如果我添加以下内容:

$authentication->loginAction()->captureTo('test')

'index.phtml' 显示字符串content".

The 'index.phtml' shows a string "content".

我读到我可能需要在将 ViewModel 分配给视图变量theloginform"之前渲染它,但我似乎无法让它工作,我尝试了以下方法但没有成功.

I have read that i may need to render the ViewModel before i assign it to the view variable 'theloginform', but i can't seem to get it to work, i have tried the following with no luck.

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 文件是否位于正确的路径:

I have even tried adding the DI into the autoload_classmap.php file but still get the same error, i have double checked the login.phtml file is at the correct path:

'/Login/view/login/login/login.phtml' 我什至把它复制到 '/Login/src/Login/view/login.phtml'

'/Login/view/login/login/login.phtml' I even copied it to '/Login/src/Login/view/login.phtml'

非常困惑已阅读然后重新阅读 Zend 文档,我只是想将一个视图传递给另一个视图...

Very confused have read then re-read the Zend documentation, i just want to pass a view to another view...

推荐答案

在 ZF 1.x 中,我可能会建议您构建一个引用视图占位符的操作助手或回调到表单的 loginAction 的控制器插件逻辑.

In ZF 1.x I would likely recommend you build an action helper that is referenced to a view placeholder or a controller plugin that calls back to loginAction for the form logic.

在 Zf2 中,看起来动作助手已被 controller plugins 并且似乎是通过 事件管理器,并且可能需要了解一个或多个"经理".但是 占位符视图助手仍然存在,甚至看起来有些熟悉.

In Zf2 it looks like action helpers have been replaced by controller plugins and seem to be triggered through the event manager and may need to be aware of one or more of the "managers". However the placeholder view helper still exists and even seems somewhat familiar.

我建议您考虑为您的登录表单显示构建/调整控制器插件,然后可以将其附加到占位符视图助手.如果幸运的话,您也许可以通过视图助手获得所需的功能.

I would suggest you look into building/adapting a controller plugin for your login form display that can then be attached to a placeholder view helper. You might be able to get the required functionality with just a view helper, if you're lucky.

我希望我能帮上更多的忙,但我自己仍然在处理这个烂摊子.

I wish I could help more, but I'm still wading through this mess myself.

祝你好运.

这篇关于Zend Framework 2 在视图中显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆