沃梦达 / 编程问答 / php问题 / 正文

ZF2 - 使用导航视图助手的多个导航菜单

ZF2 - multiple nav menus using the navigation view helper(ZF2 - 使用导航视图助手的多个导航菜单)

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

问题描述

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

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

在我的 layout 中,我这样调用视图助手:

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() 视图助手 不止一次,它只输出 second一个 在两个地方.换句话说,它正在为 both 主导航 子导航菜单打印 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.

我的合并配置如下所示:

My merged config looks like this:

'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')ZendViewHelperNavigation 会创建容器main_navigation".如果你然后调用 menu() 第一次 ZendViewHelperNavigationMenu 对象被创建直接 容器被注入.

The menu, breadcrumbs, sitemap and links helpers are registered as plugins. If you call $this->navigation('main_navigation') for the first time, the ZendViewHelperNavigation creates the container "main_navigation". If you then call menu() for the first time the ZendViewHelperNavigationMenu 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 - 使用导航视图助手的多个导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ZF2 - 使用导航视图助手的多个导航菜单

基础教程推荐