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

Hello i am new with the FrameWork and my problem concern my nav

1: when my user is logged i dont want to see this tab : [‘label’ => ‘Créer un compte’, ‘url’ => [‘/site/signup’]] and the login

so i made this for my probleme and it work well :

Yii::$app->user->isGuest
? [
       ['label' => 'ouverture de session', 'url' => ['/site/login']],
       ['label' => 'Créer un compte', 'url' => ['/site/signup']]
   ]

2:
when i am logged is work fine but now when is time to loggout is receive a error message : The ‘label’ option is required. [‘/site/logout’] not working…

see my full code : header nav

<header id="header">
        <?php
        NavBar::begin([
            'brandLabel' => Html::img('@web/img/logo.png', ['alt' => 'Charte De Crypto', 'style' => 'height: 48px;margin-right: 10px;']) . "<h3 class='h3mobile;' style='font-weight: bold;color: white;'>" . Yii::$app->name . "</h3>",
            'brandUrl' => Yii::$app->homeUrl,
            'options' => ['class' => 'mynav  fixed-top']
        ]);
        echo Nav::widget([
            'options' => ['class' => 'navbar-nav '],
            'items' => [
                ['label' => 'Home', 'url' => ['/site/index']],
                ['label' => 'À Propos', 'url' => ['/site/about']],
                ['label' => 'Nous Contacter', 'url' => ['/site/contact']],
                //['label' => 'Créer un compte', 'url' => ['/site/signup']],
                // ['label' => 'Dire', 'url' => ['/site/dire']],
                Yii::$app->user->isGuest
                    ? [
                        ['label' => 'ouverture de session', 'url' => ['/site/login']],
                        ['label' => 'Créer un compte', 'url' => ['/site/signup']],
                    ]

                    : '<li class="nav-item">'
                    . Html::beginForm(['/site/logout'])
                    . Html::submitButton(
                        'Se déconnecter (' . Yii::$app->user->identity->username . ')',
                        ['class' => 'nav-link btn btn-link logout']
                    )
                    . Html::endForm()
                    . '</li>'
            ]
        ]);
        NavBar::end();
        ?>
    </header>

so what i need to do for fixing this problem

sincerely I find this very complicated for a logic that is simple

if example the code returns to the basic one everything works fine but just the fact that I added an additional condition inside the ? everything starts not to work

error here : localhost — Postimages

Would you please try this?

Yii::$app->user->isGuest
    ? ['label' => 'ouverture de session', 'url' => ['/site/login']],
      ['label' => 'Créer un compte', 'url' => ['/site/signup']]
    : '<li class="nav-item">'
      ...

You’ve added an extra pair of parens around the last 2 items (login and signup), and it must be the cause of the problem.

i tried this way yesterday and not working the second label have an error (redline) this why i use this [ ] and put the two label inside.

my code : Screenshot 1 — Postimages

I have to applologize to you.

I’m not very sure, but probably there’s no way to set 2 and more values in ternary operator.

This won’t work:

Yii::$app->user->isGuest
    ? [
        ['label' => 'ouverture de session', 'url' => ['/site/login']],
        ['label' => 'Créer un compte', 'url' => ['/site/signup']],
    ]
    : '<li class="nav-item"> ... '

And this won’t work also:

Yii::$app->user->isGuest
    ? (
        ['label' => 'ouverture de session', 'url' => ['/site/login']],
        ['label' => 'Créer un compte', 'url' => ['/site/signup']]
    )
    : '<li class="nav-item"> ... '

And braces also won’t work:

Yii::$app->user->isGuest
    ? {
        ['label' => 'ouverture de session', 'url' => ['/site/login']],
        ['label' => 'Créer un compte', 'url' => ['/site/signup']]
    }
    : '<li class="nav-item"> ... '

So, this is what I would do in this case:

$items = [
    ['label' => 'Home', 'url' => ['/site/index']],
    ['label' => 'À Propos', 'url' => ['/site/about']],
    ['label' => 'Nous Contacter', 'url' => ['/site/contact']],
];
if (Yii::$app->user->isGuest) {
    $items[] = ['label' => 'ouverture de session', 'url' => ['/site/login']];
    $items[] = ['label' => 'Créer un compte', 'url' => ['/site/signup']];
} else {
    $items[] = '<li class="nav-item">' 
    . Html::beginForm(['/site/logout'])
    . Html::submitButton(
        'Se déconnecter (' . Yii::$app->user->identity->username . ')',
        ['class' => 'nav-link btn btn-link logout']
    )
    . Html::endForm()
    . '</li>';
}
echo Nav::widget([
    'options' => ['class' => 'navbar-nav '],
    'items' => $items,
]);

You are the man who was sent by god to solve my problem is working perfecly well !

today on my side i have tested an another way but not working too. if i clicked on logout i receive : Method Not Allowed (#405) Method Not Allowed. This URL can only handle the following request methods: POST

the important thing is i have tried my best.

    'items' => [
        ['label' => 'Home', 'url' => ['/site/index']],
        ['label' => 'À Propos', 'url' => ['/site/about']],
        ['label' => 'Nous Contacter', 'url' => ['/site/contact']],
        //['label' => 'Créer un compte', 'url' => ['/site/signup']],
        // ['label' => 'Dire', 'url' => ['/site/dire']],
        [   'label' => 'ouverture de session', 
            'url' => ['/site/login'], 
            'visible' => Yii::$app->user->isGuest
        ],
        [   'label' => 'Créer un compte', 
            'url' => ['/site/signup'], 
            'visible' => Yii::$app->user->isGuest
        ],
        [   'label' => 'Se déconnecter (' . Yii::$app->user->identity->username . ')', 
            'url' => ['/site/logout'], 
            'visible' => !Yii::$app->user->isGuest
        ],
    ]

thanks you for your time and fixing my problem.

1 Like

Oh, that’s nice. I didn’t know that we could set visible attribute to each item.

But the logout link looks difficult, because it must be a submit button in a form and probably we have to use a string to define the item and can’t use the array style of item definition.

1 Like