Yii2 如何使用 isGuest 将新的临时项目添加到 NavBar 项目
Yii2 How to add a new temporary item to NavBar items using isGuest
我是 Yii2 的新手,最近下载了该框架的基本应用版本。
“main.php”文件中有一段代码:
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'Main', 'url' => ['/site/index']],
['label' => 'Countries', 'url' => ['/country/countries']],
['label' => 'About us', 'url' => ['/site/about']],
['label' => 'Contacts', 'url' => ['/site/contact']],
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/user/login']]
) : (
'<li>'
. Html::beginForm(['/user/logout'], 'post')
. Html::submitButton(
'Logout(' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>'
)
]
]);
我想在其中添加注册 link 像这样:
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/user/login']],
['label' => 'Register', 'url' => ['/user/registration']]
) : (
'<li>'
. Html::beginForm(['/user/logout'], 'post')
. Html::submitButton(
'Logout(' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
我知道这段代码是错误的,但我想问一下,我怎样才能正确地做到这一点?
您的尝试无效,因为您使用的三元运算符只允许 1 个值而不是 2 个值。登录和注册是2个数组。
可能的解决方案:
// What everybody sees
$items = [
['label' => 'Main', 'url' => ['/site/index']],
['label' => 'Countries', 'url' => ['/country/countries']],
['label' => 'About us', 'url' => ['/site/about']],
['label' => 'Contacts', 'url' => ['/site/contact']],
];
// What only guests see
if (Yii::$app->user->isGuest) {
$items[] = ['label' => 'Registration', 'url' => ['/user/registration']];
$items[] = ['label' => 'Login', 'url' => ['/user/login']];
}
// What logged in users see
else {
$items[] = '<li>'
. Html::beginForm(['/user/logout'], 'post')
. Html::submitButton(
'Logout(' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>';
}
// Echo your navigation
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $items,
]);
我是 Yii2 的新手,最近下载了该框架的基本应用版本。 “main.php”文件中有一段代码:
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'Main', 'url' => ['/site/index']],
['label' => 'Countries', 'url' => ['/country/countries']],
['label' => 'About us', 'url' => ['/site/about']],
['label' => 'Contacts', 'url' => ['/site/contact']],
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/user/login']]
) : (
'<li>'
. Html::beginForm(['/user/logout'], 'post')
. Html::submitButton(
'Logout(' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>'
)
]
]);
我想在其中添加注册 link 像这样:
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/user/login']],
['label' => 'Register', 'url' => ['/user/registration']]
) : (
'<li>'
. Html::beginForm(['/user/logout'], 'post')
. Html::submitButton(
'Logout(' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
我知道这段代码是错误的,但我想问一下,我怎样才能正确地做到这一点?
您的尝试无效,因为您使用的三元运算符只允许 1 个值而不是 2 个值。登录和注册是2个数组。
可能的解决方案:
// What everybody sees
$items = [
['label' => 'Main', 'url' => ['/site/index']],
['label' => 'Countries', 'url' => ['/country/countries']],
['label' => 'About us', 'url' => ['/site/about']],
['label' => 'Contacts', 'url' => ['/site/contact']],
];
// What only guests see
if (Yii::$app->user->isGuest) {
$items[] = ['label' => 'Registration', 'url' => ['/user/registration']];
$items[] = ['label' => 'Login', 'url' => ['/user/login']];
}
// What logged in users see
else {
$items[] = '<li>'
. Html::beginForm(['/user/logout'], 'post')
. Html::submitButton(
'Logout(' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>';
}
// Echo your navigation
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $items,
]);