Hi,
I am learning Yii2 and I have one question regarding Nav::widget in yii-basic app.
In "views/layouts/main.ph", I see below code for Nav::widget "items" property:
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']]
The logic is easy, for guest user, it will show “login” and for login-ed users, show “logout…”. Consider a more practical case for most website, it will show “login” AND “signup” for guest, how to achieve that? I’ve write as below but failed:
Yii::$app->user->isGuest ?
['label' => 'xx'] , ['label' => 'zz'] :
['label' => 'yy'],
I also tried other code, but cannot figure it out. Also, I found I cannot use "if else" to replace "?:" logic inside it…
Is this a known limitation of Nav::widget? Any suggestions how you implement for this kind of logic?
By the way, something like below can work well, but code may be ugly for complex logic, I guess:
if(Yii::$app->user->isGuest) {
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'MyLogin', 'url' => ['/site/my-login']],
['label' => 'MySignup', 'url' => ['/site/my-signup']],
],
]);
} else {
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'MyLogout', 'url' => ['/site/my-logout'], 'linkOptions' => ['data-method' => 'post'],],
],
]);
}
Thanks,
Shenghong