如何在yii bootstrap小部件中获取两个链接?

时间:2016-07-14 11:34:16

标签: php yii twitter-bootstrap-3 yii2

如何在Yii2的bootstrap小部件中显示注册和登录链接?

我是Yii框架中的新手

  

错误:语法错误,意外' =>' (T_DOUBLE_ARROW),期待']'

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']],
        Yii::$app->user->isGuest ? (
        ['label' => 'Login', 'url' => ['/site/login']]
        ['label' => 'Register', 'url' => ['/site/Register']]
        ) : (
            '<li>'
            . Html::beginForm(['/site/logout'], 'post', ['class' => 'navbar-form'])
            . Html::submitButton(
                'Logout (' . Yii::$app->user->identity->username . ')',
                ['class' => 'btn btn-link']
            )
            . Html::endForm()
            . '</li>'
        )
    ],
]);
  

逗号没有帮助:语法错误,意外&#39;,&#39;

3 个答案:

答案 0 :(得分:1)

尝试,此代码正常运行。

<?php
NavBar::begin([
    'brandLabel' => 'My Company',
    'brandUrl' => Yii::$app->homeUrl,
    'options' => [
        'class' => 'navbar-inverse navbar-fixed-top',
    ],
]);
$menuItems = [
    ['label' => 'Home', 'url' => ['/site/index']],
    ['label' => 'About', 'url' => ['/site/about']],
    ['label' => 'Contact', 'url' => ['/site/contact']],
];
if (Yii::$app->user->isGuest) {
    $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
    $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
    $menuItems[] = '<li>'
        . Html::beginForm(['/site/logout'], 'post')
        . Html::submitButton(
            'Logout (' . Yii::$app->user->identity->username . ')',
            ['class' => 'btn btn-link']
        )
        . Html::endForm()
        . '</li>';
}
echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'items' => $menuItems,
]);
NavBar::end();
?>

答案 1 :(得分:1)

试试这个

<?php
    $items[]=[
            ['label' => 'Home', 'url' => ['/site/index']],
            ['label' => 'About', 'url' => ['/site/about']],
            ['label' => 'Contact', 'url' => ['/site/contact']]
        ];
           if( Yii::$app->user->isGuest) {
           $items[]= ['label' => 'Login', 'url' => ['/site/login']];
             $items[]= ['label' => 'Register', 'url' => ['/site/Register']];
           }else{
               $items[]= '<li>'
                . Html::beginForm(['/site/logout'], 'post', ['class' => 'navbar-form'])
                . Html::submitButton(
                    'Logout (' . Yii::$app->user->identity->username . ')',
                    ['class' => 'btn btn-link']
                )
                . Html::endForm()
                . '</li>';
           }

    echo Nav::widget([
        'options' => ['class' => 'navbar-nav navbar-right'],
        'items' => $items
    ]);
    ?>

答案 2 :(得分:-1)

你应该在这里添加逗号

Yii::$app->user->isGuest ? (
    ['label' => 'Login', 'url' => ['/site/login']],  
                                                  ^  
    ['label' => 'Register', 'url' => ['/site/Register']]