Zend Framework 2 Navigation Submenu (Main Menu)

I am creating a menu using ZF2 and bootstrap, and I already have a menu like this:

Home | Users | Options

But now I need submenus, but I could not find a way to do this. I need something like hover over a menu item (Ex: User) and then show "List", "Add", "Change"

I would really appreciate any help.

thanks

+4
source share
1 answer

You can achieve this with a partial view.

In the configuration file, for example. config / autoload / global.php:

return array(

    // Your others config arrays

    'navigation' => array(
        'default' => array(
            array(
                'label' => 'Home',
                'route' => 'home',
            ),
            array(
                'label' => 'User',
                'route' => 'user',
                'pages' => array(
                    array(
                        'label' => 'List',
                        'route' => 'list',
                    ),
                    array(
                        'label' => 'Add',
                        'route' => 'add',
                    ),
                    array(
                        'label' => 'Edit',
                        'route' => 'edit',
                    ),
                ),
            ),
            array(
                'label' => 'Options',
                'route' => 'options',
            ),
        )
    )
);

In your layout file, for example. view / layout / layout.phtml:

<nav>
    <?php
    echo $this->navigation('navigation')
              ->menu()
              ->setPartial('partial/menu')
              ->render();
    ?>
</nav>

Partial view, here view / partial / menu.phtml:

<ul>
<?php
foreach ($this->container as $page)
{
    $hasChildren = $page->hasPages();
    if( ! $hasChildren)
    {
        ?>
        <li><a href="<?php echo $page->getHref(); ?>"><?php echo $page->getLabel(); ?></a></li>
        <?php
    }
    else
    {
        ?>
        <li>
            <a href="<?php echo $page->getHref(); ?>"><?php echo $page->getLabel(); ?></a>
            <ul>
            <?php
            foreach($page->getPages() as $child)
            {
                ?>
                <li><a href="<?php echo $child->getHref(); ?>"><?php echo $child->getLabel(); ?></a></li>
                <?php
            }
            ?>
            </ul>
        </li>
        <?php
    }
}
?>
</ul>

CSS, :

fooobar.com/questions/1520622/...

+4

All Articles