ZF2-使用导航视图帮助器的多个导航菜单 [英] ZF2 - multiple nav menus using the navigation view helper

查看:74
本文介绍了ZF2-使用导航视图帮助器的多个导航菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将主导航与子菜单结合使用以进行更具体的导航.

I am trying to use a main navigation in combination with a submenu for more specific navigating.

在我的布局中,我这样称呼视图助手:

In my layout I am calling the view helper like this:

$this->navigation('main_navigation')->menu()

在我的视图中,我这样称呼它:

and in my view I am calling it like this:

$this->navigation('sub_navigation')->menu()

问题是,每当我多次将navigation()视图助手称为 时,它只会同时输出第二个的地方.换句话说,它将同时为主导航子导航菜单打印 subnav .

The problem is that whenever I call the navigation() view helper a more than once, it just outputs the second one in both places. In other words, it's printing the subnav for both the main nav and the subnav menus.

我的合并配置如下:

'navigation' => array(
    'main' => array(
        'home' => array(
            'label' => 'Home',
            'route' => 'myroute',
        ),
        'somepage' => array(
            'label' => 'Me',
            'route' => 'somepage'
        )
    ),
    'sub' => array(
        'test' => array(
            'label'  => 'Test',
            'route'  => 'myroute',
            'action' => 'test'
        ),
        'other-test' => array(
            'label'  => 'Other Test',
            'route'  => 'myroute',
            'action' => 'other-test'
        )
    )
)

如何使用navigation视图助手,以便为每个呼叫打印正确的菜单?

How do I use the navigation view helper so that it will print the correct menu for each call?

推荐答案

menubreadcrumbssitemaplinks帮助器已注册为插件.如果您是第一次调用$this->navigation('main_navigation'),则Zend\View\Helper\Navigation将创建容器"main_navigation".如果您随后第一次调用menu() ,则会直接创建Zend\View\Helper\Navigation\Menu对象,并注入容器.

The menu, breadcrumbs, sitemap and links helpers are registered as plugins. If you call $this->navigation('main_navigation') for the first time, the Zend\View\Helper\Navigation creates the container "main_navigation". If you then call menu() for the first time the Zend\View\Helper\Navigation\Menu object is created and directly the container is injected.

这表明存在缺陷:如果立即调用$this->navigation('sub_navigation'),则导航容器将加载到navigation()视图帮助器中.当您再调用menu()时,菜单视图助手已经创建.因此,不再注入新容器.

This indicates the flaw: if you call $this->navigation('sub_navigation') now, the navigation container is loaded in the navigation() view helper. When you then call menu(), the menu view helper is already created. So the new container is not injected anymore.

很明显,这是代码库中的错误.有一个快速修复:菜单助手也可以接受容器字符串:

Clearly this is a bug in the code base. There is one quick fix: the menu helper can also accept the container string:

<?php echo $this->navigation()->menu('main_navigation'); ?>
<?php echo $this->navigation()->menu('sub_navigation'); ?>

我已经提出了一个问题,该错误将得到解决.

I have filed an issue about it and the bug will be fixed.

这篇关于ZF2-使用导航视图帮助器的多个导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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