mercredi 11 novembre 2015

Shorthand if in PHP Array

shorthand if in a PHP array won't work, even if I surround the true in braces.

The working code:

echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'items' => [ 
        Yii::$app->user->isGuest ?
            ['label' => 'Login', 'url' => ['/site/login']] :
            [
                'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
                'url' => ['/site/logout'],
                'linkOptions' => ['data-method' => 'post']
            ],
    ],
]);

The not working code:

echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'items' => [ 
        Yii::$app->user->isGuest ?
            (['label' => 'Registrieren', 'url' => ['/site/register']],
            ['label' => 'Login', 'url' => ['/site/login']]) :
            [
                'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
                'url' => ['/site/logout'],
                'linkOptions' => ['data-method' => 'post']
            ],
    ],
]);

I understand what the problem is, but can't figure out a workarround for it. Any ideas?

Thanks

Aucun commentaire:

Enregistrer un commentaire