问题
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