KnpMenuBundle does not work with Bootstrap 4 navbar

I am currently creating a menu with the Symfony package: KnpMenuBundle. I am using Bootstrap 4 as a style sheet.

Bootstrap 4 requires each list item in the navigation bar to have a nav-item class:

<li class="nav-item active"> <-- this
    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>

I cannot figure out how to add the "nav-item" class to a list item using KnpMenuBundle. I currently see this when I load a page: navigation result

This is my Builder class in src / AppBundle / Menu:

namespace AppBundle\Menu;


use Knp\Menu\MenuFactory;

class Builder
{
    public function mainMenu(MenuFactory $factory, array $optioins)
    {
        $menu = $factory->createItem('root');
        $menu->setChildrenAttribute('class', 'navbar-nav mr-auto');
        $menu->addChild('Home', ['route' => 'homepage']);
        $menu->setChildrenAttributes(['class' => 'nav-item']);
        return $menu;
    }
}

My code in the base.html.twig file for generating the menu:

{{ knp_menu_render('AppBundle:Builder:mainMenu', {'currentClass': 'active'}) }}

What should I do to make it work?

+4
source share
2 answers

I did this to get the right level. Not yet developed drop-down lists.

$menu->setChildrenAttribute('class', 'navbar-nav');
// menu items
foreach ($menu as $child) {
    $child->setLinkAttribute('class', 'nav-link')
        ->setAttribute('class', 'nav-item');
}
+2

:

$menu->addChild('Home', ['route' => 'homepage'])
     ->setAttributes(array(
         'class' => 'nav-item'
     ));
+2

Source: https://habr.com/ru/post/1686295/


All Articles