Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Menu Bar Visibility to a Specific User

In my main.php, I have a menu:

'items' => [
    ['label' => 'Home', 'url' => ['/site/index']],
    ['label' => 'Biz Staff', 'url' => ['bizstaff/index'], 'visible' => User::isBizAdmin(), 'items' => [
        ['label' => 'Staff List', 'url' => 'index.php?r=user/index'],
        ['label' => 'Add Staff', 'url' => 'index.php?r=user/create'],
    ]],

    ['label' => 'Transaction', 'url' => ['transactions/index'], 'visible' => User::isBizAdmin() || User::isBizStaff(), 'items' => [
        ['label' => 'Transactions', 'url' => 'index.php?r=transactions/index'],
        ['label' => 'Add Transactions', 'url' => 'index.php?r=transactions/create'],
    ]],

    Yii::$app->user->isGuest ?
        ['label' => 'Login', 'url' => ['/site/login']] :
        ['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
          'url' => ['/site/logout'],
          'linkOptions' => ['data-method' => 'post']
        ],
    ],

Here, I am logged in as the superadmin (note that only Home and Logout menu is visible to superadmin user). Inside the homepage (Home menu) is a List of Biz Admins which is placed inside a GridView widget. It has an Action column where view, update and delete icons are placed. When I click the view icon of a specific Biz Admin, it will then render a detailed view page of that Biz Admin where its Store Name and List of Staff are seen. In this page, there is a View Store button which will redirect to the bizadmin view/page.

Whenever superadmin lands on bizadmin view/page, the menu bar should now change to:

Home, Biz Staff, Transaction, Logout

How do I do this? Is it set in the visible attribute? Any of your answers would be highly appreciated. I am currently stuck in this problem.

like image 343
kaynewilder Avatar asked Sep 28 '22 17:09

kaynewilder


1 Answers

Yes. You should use visible property. 'visible' => true or 'visible' => false.

Or you may assemble an array. Like that:

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[] = [
                    'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
                    'url' => ['/site/logout'],
                    'linkOptions' => ['data-method' => 'post']
                ];
            }
            echo Nav::widget([
                'options' => ['class' => 'navbar-nav navbar-right'],
                'items' => $menuItems,
            ]);
            NavBar::end();

See in Advanced template - https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/views/layouts/main.php

like image 80
vitalik_74 Avatar answered Oct 03 '22 01:10

vitalik_74