D
D
dev4002016-11-23 20:53:24
Yii
dev400, 2016-11-23 20:53:24

Strange code in Nav yii2, how can I do it alternatively?

Found something like this

Yii::$app->user->isGuest ? "" : (
                            ['label' => 'Главная', 'url' => ['/site/index']] 
                        ),
                        Yii::$app->user->isGuest ? "" : (
                            ['label' => 'Сотрудники', 'url' => ['/site/about']]),
                        
                        Yii::$app->user->isGuest ? "" : (
                            ['label' => 'Лиды', 'url' => ['/site/contact']]),
                        
                        Yii::$app->user->isGuest ? "" : (
                            ['label' => 'Заказы', 'url' => ['/site/contact']]),
                        
                        Yii::$app->user->isGuest ? "" : (
                            ['label' => 'Настройки', 'url' => ['/site/contact']]),

And it is impossible to group them into one condition ... How can this horror be remade?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bears, 2016-11-23
@dev400

There is most likely something like this:

$nav = [
    'items' => Yii::$app->user->isGuest ? '' : (
                    ['label' => 'Главная', 'url' => ['/site/index']] 
                ),
                Yii::$app->user->isGuest ? "" : (
                    ['label' => 'Сотрудники', 'url' => ['/site/about']]),
                
                Yii::$app->user->isGuest ? "" : (
                    ['label' => 'Лиды', 'url' => ['/site/contact']]),
                
                Yii::$app->user->isGuest ? "" : (
                    ['label' => 'Заказы', 'url' => ['/site/contact']]),
                
                Yii::$app->user->isGuest ? "" : (
                    ['label' => 'Настройки', 'url' => ['/site/contact']]),
];

This is the same as:
if (Yii::$app->user->isGuest) {
    $nav = [
        'items' => [],
    ];
} else {
    $nav = [
        'items' => [
            ['label' => 'Главная', 'url' => ['/site/index']] ,
            ['label' => 'Сотрудники', 'url' => ['/site/about']],
            ['label' => 'Лиды', 'url' => ['/site/contact']],
            ['label' => 'Заказы', 'url' => ['/site/contact']],
            ['label' => 'Настройки', 'url' => ['/site/contact']],
        ],
    ];
}

Some pervert wrote the code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question