ZF2 Nav replace Params in view

2019-08-30 23:29发布

问题:

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>

回答1:

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

(...)
'template_map' => array(
    'navigation' => __DIR__ . '/../view/module/navigation.phtml',
(...)

layout

//GET THE PARTIAL
$partial = array( 'navigation', 'module' );

//APPLY THE PARTIAL TO THE MENU (THE NAV CLASS WILL USE IT TO RENDER IT)
$this->navigation( 'admin_navigation' )->menu()->setPartial( $partial );

//WRITE THE MENU
echo $this->navigation( 'admin_navigation' )->menu()

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

//I GET THE Zend\View\Helper\Url CLASS TO CREATE THE ROUTE AS USUAL
$url = $this->getHelperPluginManager()->get( 'url' );

//LOOP ALL THE PAGES IN THE 'admin_navigation' NAVIGATION MENU
foreach ( $this->container as $page ) {

    //USE YOUR CUSTOM HTML TO CREATE THE MENU ITEM

    //WHEN I GET TO THE NAVIGATION ITEM WHERE I NEED TO ADD A PARAMETER...
    (...)
    $href = $url( $page->getRoute(), array( 'userId' => $this->zfcUserIdentity()->getId();
    (...)
}

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.



回答2:

This is my solution, but it's a dirty mess...

module.config.php

return array(
'navigation' => array(
    'default' => array(
        'overview' => array(
            'label'  => 'Edit Item',
            'route'  => 'item/default',
            'action' => 'Edit',
            'params' => array(
                'id' => '99',
            ),
        ),
    ),
),
);

page.phtml

$renderNav = $this->navigation()->menu()->render();
echo str_replace('99', $item->getId(), $renderNav);