Conditions in Navbar

Hello,

I need more info for conditionals items in navbar. Yes, I searched but nothing found.


        <?php

            NavBar::begin([

                'brandLabel' => 'Basic',

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

                'options' => [

                    'class' => 'navbar-inverse navbar-fixed-top',

                ],

            ]);

            echo Nav::widget([

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

                'items' => [

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

                    ['label' => 'About', 'url' => ['/site/about']],

                    ['label' => 'Contact', 'url' => ['/site/contact']],

Up to here, it is all clear, but not the part below:


           Yii::$app->user->isGuest ?

                        ['label' => 'Login', 'url' => ['/user/login']] :

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

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

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

                ],

            ]);

            NavBar::end();

        ?>

It is clear, when a user is logged out, in the navbar is visible the “login” item, then the user is logged in, will be visible the “logout” item. But is not a classic php if/then construct, neither a real alternative conditional syntax. So I’m confused. Is there a manual page or are there more examples where it is explained? I need to add other conditionals items, understand the properties of this condition, if I can nest, etc.

If I understand your question correctly, you don’t understand the ?: syntax?

?: (or Ternary operator) is a part of PHP that is shorthand for doing an if/else statement. You can find information about it in PHP’s manual - http://php.net/manual/en/language.operators.comparison.php#example-137

Here is an example of that same code with more menus that might help:




'items' => [

			Yii::$app->user->isGuest ?

			['label' => 'User Login', 'url' => ['#'], 'items' => [

				['label' => 'Create an Account', 'url' => '/user/register'],

				['label' => 'Sign In', 'url' => '/user/login'],

			]] :

			['label' => 'Welcome '. Yii::$app->user->identity->username, 'items' => [

				'<li role="presentation" class="divider"></li>',

				['label' => 'Account Settings'],

				['label' => 'My Sales', 'url' => '/sales/'],

				['label' => 'My Account', 'url' => '/user/'],

				'<li role="presentation" class="divider"></li>',

				['label' => 'Logout', 'url' => '/user/logout'],

			]]

		]



Thank you very much, Keith.

Good evening,

How do I check if it is a certain user?

Example:




    NavBar::begin([

        'brandLabel' => Yii::$app->params['nome'],

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

        'options' => [

            'class' => 'navbar-inverse navbar-fixed-top',

            'style' => Yii::$app->user->isGuest ? 'visibility: hidden' : '',

         ],

    ]);

    echo Nav::widget([

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

    'items' => [


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


        # How do I do that?

        # if($user->id == '1') {

        #    [

        #        'label' => 'Admin',

        #        'url' => ['/admin/index']

        #    ],

        # },


        ['label' => 'Contato', 'url' => ['/site/contact']],

            Yii::$app->user->isGuest ?

            ['label' => 'Login', 'url' => ['/site/login']] :

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

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

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

            ],

    ]);

NavBar::end();



You just need to construct ‘items’ array according to your logic.

If it’s complicated, you can write it outside of the widget. You don’t need to write it inline.

For example, something like this would do:




    $items = [];

    $items[] = ['label' => 'Inicio', 'url' => ['/site/index']];

    if (Yii::$app->user->id == '1') {

        $items[] = ['label' => 'Admin', 'url' => ['/admin/index']];

    }

    $items[] = ['label' => 'Contato', 'url' => ['/site/contact']];

    $items[] = Yii::$app->user->isGuest ?

        ['label' => 'Login', 'url' => ['/site/login']] :

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

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

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

        ];


    echo Nav::widget([

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

        'items' => $items

    ]);