I am trying to create a dynamic reusable menu for use on my application. I have a very basic route that has :id in it which represents the record id. In the submenu I want this :id to be replace dynamically with the record that the user is currently interacting with. Please see below for complete details.
Here is my Route
'item' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Item/',
'defaults' => array(
'__NAMESPACE__' => 'ReserveAnything\Item\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[:action][/][:id]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
Here is my navigation file
return array(
'navigation' => array(
'default' => array(
'overview' => array(
'label' => 'Edit Item',
'route' => 'item/default',
'action' => 'Edit',
'params' => array(
'id' => 0,
)
),
'extras' => array(
'label' => 'Manage Extras',
'route' => 'item/default',
'action' => 'Extras',
'params' => array(
'id' => 0,
),
),
),
);
And finally here is my view
echo $this->navigation('navigation')->menu();
Now the output of this is
<ul class="navigation">
<li>
<a href="/Item/Edit/0">Edit Item</a>
</li>
<li>
<a href="/Item/Extras/0">Manage Extras</a>
</li>
Now this would be prefect if I was on Id 0 however if I am on Id 1 (/Item/Edit/1) it would obviously still appears in the above example. How can I get the menu to update the Id inside the config. I imagine this can be done. My Ideal output for when I was editing record Id 1 would be
<ul class="navigation">
<li>
<a href="/Item/Edit/1">Edit Item</a>
</li>
<li>
<a href="/Item/Extras/1">Manage Extras</a>
</li>
I faced a similar issue when I wanted a navigation item which allows each logged user to change its data (I need to add to the route the user id). I solved it adding a partial to the navigation object. You can find info about that here.
I do it this way:
module.config.php
layout
Partial file
The $this object in this file will contain a Zend\View\Model\ViewModel class which has an array with all the pages. You could get other info, like I do. I use the ZfcUser module, and I can get the logged user id through $this->zfcUserIdentity().
That's all. If you like the menu code generated by the zf2 navigation menu class, copy it from your browser and adapt your partial. Or you could use the output you posted on your question, as you wish.
This is my solution, but it's a dirty mess...
module.config.php
page.phtml