I want ask you about this problem. What do i need to create Dynamic Menu with Zend\Navigation\Navigation?
In ZF1 i made like this:
$container = new Zend_Navigation();
$pages = array(
array(
'label' => 'Save',
'action' => 'save',
),
array(
'label' => 'Delete',
'action' => 'delete',
),
);
// add two pages
$container->addPages($pages);
and then in view:
$this->navigation()->menu();
But in ZF2 pages are taking from config. Now i create \config\autoload\nav.global.php and here create page array. But i need to do page array in method and send it into navigation helper, but ii dont know how ((
i tried to do this in my controller:
use Zend\Navigation\Navigation;
$pages =array(
// All navigation-related configuration is collected in the 'navigation' key
'navigation' => array(
// The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
'default' => array(
// And finally, here is where we define our page hierarchy
'account' => array(
'label' => 'faq',
'route' => 'faq',
'pages' => array(
'news' => array(
'label' => 'news',
'route' => 'news',
),
'manual' => array(
'label' => 'manual',
'route' => 'manual',
),
),
),
),
),
);
$Menu = new Navigation($pages);
and then this in view:
$this->Menu()->menu();
but i have a lot of mistakes...
i think you understand my problem. please help. sorry for my english.
You need to do so
In the controller
In view
My previous answer is not correct. The following code works. For one page. In controller, edit action:
For some backgrounds you might read this answer on another, but similar question regarding
Zend\Navigation
. The point is you want MVC pages and MVC pages in Zend Framework 2 need a way to assemble the url and find our if the url is active or not.Every MVC page has a route name. The route stack routes the request and gets a route match. You must inject this route match into the navigation, so every page could check its own route against the matched one.
Similar for the url assembly. If you want to convert the route name into an url, you need the route stack ('router'). Inject the router in your application too and you are able to assemble.
In short:
And similar as above answer, in your view:
Eremite answer is not really wrong. I have tested all the recommendations given here and actually when the default route have several child_routes mark as default, menus doesn't mark the active flag correctly. So it is needed to pass the matched route as a parameter. But perhaps I'm doing something wrong.
Cheers