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?
The
menu
,breadcrumbs
,sitemap
andlinks
helpers are registered as plugins. If you call$this->navigation('main_navigation')
for the first time, theZend\View\Helper\Navigation
creates the container "main_navigation". If you then callmenu()
for the first time theZend\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 thenavigation()
view helper. When you then callmenu()
, 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:
I have filed an issue about it and the bug will be fixed.