is there a tutorial for use navbar or how to add items by array?

Hello, I’m very new at Yii2 development and I’m looking for a tutorial who shows me how to set up a navbar correctly, I already look for in google and actually can’t find any useful resource.

Here’s what I’ve done already:




<?php

                NavBar::begin([

                    'brandUrl' => Yii::$app->homeUrl,

                    'options' => [

                        'class' => 'navbar navbar-static-top',

                        'role' => 'navigation',

                    ],

                ]);

                

                $menuItems = [

                    ['label' => 'Home', 'url' => ['/site/index']],

                ];

                if (Yii::$app->user->isGuest) {

                    $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];

                } else {

                    $menuItems[] = [

                        'label' => 'Logout (' . Yii::$app->user->identity->username . ')',

                        'url' => ['/site/logout'],

                        'linkOptions' => ['data-method' => 'post']

                    ];

                }

                $menuItems[] = [

                    'label'=>'dropdown',

                    ['label'=>'Usuario',

                    'class' => 'dropdown user user-menu',

                    'img'=>'dist/img/user2-160x160.jpg',]

                ];

                echo Nav::widget([

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

                    'items' => $menuItems,

                ]);

                NavBar::end();

                ?>



Hope someone can help.

Regards!

The navbar widget uses Nav widget inside of it so you can look at both docs and they show you how to use it.

Navbar docs

Nav Docs

T duplicate what you did using the widgets would be something like




<?php


use yii\bootstrap\Nav;

use yii\bootstrap\NavBar;


NavBar::begin([

	'brandLabel' => Yii::$app->name,

	'brandUrl' => Yii::$app->homeUrl,

	'options' => [

    	'class' => 'navbar navbar-static-top',

    	'role' => 'navigation',

	]

]);

echo Nav::widget([

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

	'items' => [

    	[

        	'label' => 'Home',

        	'url' => ['site/index'],

        	'linkOptions' => [],

    	],

    	[

        	'label' => 'Login',

        	'url' => ['site/login'],

        	'visible' => Yii::$app->user->isGuest

    	],

    	[

        	'label' => 'Usuario',

        	'class' => 'dropdown user user-menu',

        	'img' => 'dist/img/user2-160x160.jpg'

    	],

    	[

        	'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' => '#'

            	]

        	]

    	]

	]

]);

NavBar::end();

?>



ok thanks for your response, but I have a problem rendering the nav widget, automatically add’s my a


<div class='container'>

tag and overflows the space in between of my nav, is there any workaround?

6616

Captura de pantalla 2015-07-07 a las 23.23.07.png

that’s because of the innerContainerOptions. It’s not documented that it’s the default class but if you read the code you can see where it checks if the inner container’s css class property is set. if it’s not then it will use a default css class of “container”.

Look at lines 109 - 110. If you also notice there is a renderInnerContainer check on line 108 which means you will be able to disable it using the renderInnerContainer property.




NavBar::begin([

'renderInnerContainer'=>false  

]);

Of course you will want to keep your other settings.