ZF2 - multiple nav menus using the navigation view helper

别来无恙 提交于 2019-12-06 07:31:47

问题


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()

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'
        )
    )
)

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


回答1:


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 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.



来源:https://stackoverflow.com/questions/12984508/zf2-multiple-nav-menus-using-the-navigation-view-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!