Yii2 uses short array notation extensively. Once you get used to it, it’s great, but it can take a bit of time to read code blocks with it. When arrays are nested, it can be even more confusing. I found the Bootstrap navigation bar layouts to be hard to read. I also couldn’t easily find examples of dropdown menus in the Yii2 documentation.

navbar-dropdown

Here’s the Yii2 Navigation Bar and Bootstrap NavBar documentation.

Here’s some code that builds a Bootstrap navigation bar in Yii2 with dropdown menus:

[php]<?php
NavBar::begin([
‘brandLabel’ => Yii::t(‘frontend’,’MeetingPlanner.io’), //
‘brandUrl’ => Yii::$app->homeUrl,
‘options’ => [
‘class’ => ‘navbar-inverse navbar-fixed-top’,
],
]);
if (Yii::$app->user->isGuest) {
$menuItems[] = [‘label’ => Yii::t(‘frontend’,’Signup’), ‘url’ => [‘/site/signup’]];
$menuItems[] = [‘label’ => Yii::t(‘frontend’,’Login’), ‘url’ => [‘/site/login’]];
} else {
$menuItems = [
[‘label’ => Yii::t(‘frontend’,’Meetings’), ‘url’ => [‘/meeting’]],
[‘label’ => Yii::t(‘frontend’,’Places’), ‘url’ => [‘/place/yours’]],
];
}
$menuItems[]=[‘label’ => Yii::t(‘frontend’,’About’),
‘items’ => [
[‘label’ => Yii::t(‘frontend’,’Learn more’), ‘url’ => [‘/site/about’]],
[‘label’ => Yii::t(‘frontend’,’Contact us’), ‘url’ => [‘/site/contact’]],
],
];
if (!Yii::$app->user->isGuest) {
$menuItems[] = [
‘label’ => ‘Account’,
‘items’ => [
[
‘label’ => Yii::t(‘frontend’,’Friends’),
‘url’ => [‘/friend’],
],
[
‘label’ => Yii::t(‘frontend’,’Contact information’),
‘url’ => [‘/user-contact’],
],
[
‘label’ => Yii::t(‘frontend’,’Settings’),
‘url’ => [‘/user-setting’],
],
[
‘label’ => Yii::t(‘frontend’,’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();
?>
[/php]

Support this site, share this page:Share on twitter

Twitter

Share on reddit

Reddit

Share on linkedin

Linkedin

Share on facebook

Facebook

Share on email

Email