带有下拉菜单的 yii2 导航栏

yii2 navbar with dropdown

在默认导航栏上进行一些简单的编辑后,我得到了下面菜单的代码...如果我添加更多菜单,那将非常难看。

<?php
            NavBar::begin([
                'brandLabel' => 'My Project',
                'brandUrl' => Yii::$app->homeUrl,
                'options' => [
                    'class' => 'navbar-inverse navbar-fixed-top',
                ],
            ]);
            $menuItems = [
                ['label' => 'Home', 'url' => ['/site/index']],
                ['label' => 'Contact', 'url' => ['/site/contact'],'visible'=>false],
            ];
            if (Yii::$app->user->isGuest) {
                $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
                $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
            } else {

                if(yii::$app->user->can('admin')){
                $menuItems[] = ['label' => 'Users', 'url' => ['/users']];
                }

                $menuItems[] = ['label' => 'BUB Sub Projects', 'url' => ['/bub']];
                $menuItems[] = ['label' => 'Incoming BUB', 'url' => ['/bubincoming']];
                $menuItems[] = [
                    'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
                    'url' => ['/site/logout'],
                    'linkOptions' => ['data-method' => 'post']
                ];

            }
            echo Nav::widget([
                'options' => ['class' => 'navbar-nav navbar-right'],
                'items' => $menuItems,
            ]);
            NavBar::end();

        ?>

如何向任何菜单添加下拉菜单或子菜单?

来自官方documentation。我得到了答案。我将选项从 nav-pills 更改为 navbar-nav

echo Nav::widget([
    'items' => [
        [
            'label' => 'Home',
            'url' => ['site/index'],
            'linkOptions' => [...],
        ],
        [
            'label' => 'Dropdown',
            'items' => [
                 ['label' => 'Level 1 - Dropdown A', 'url' => '#'],
                 '<li class="divider"></li>',
                 '<li class="dropdown-header">Dropdown Header</li>',
                 ['label' => 'Level 1 - Dropdown B', 'url' => '#'],
            ],
        ],
    ],
    'options' => ['class' =>'navbar-nav'],
]);

好吧,我没有所需的声誉,但是,为了使 "nerison" 答案更正确,也添加导航 class:像这些

'options' => ['class' =>'nav navbar-nav'],