main.php view

Greetings everyone,

I would like to know how we could implement different views on the same main.php after user login?

for example: i need to show different nav tabs after user login.

Thank you and have a great day

If you just want to show different navigation to logged in user, you do not need to use different layout file.

Now, are you using RBAC to fine tune user roles ? Or you do not care about roles and just want to display different navigation to users that are authenticated ?

If you are not using RBAC you can write this code in main.php file to display some nav tabs to logged in users like this:

// if user is guest, display these links

if (Yii::$app->user->isGuest) 


    $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];

    $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];


// user is authenticated, display him these links



    $menuItems[] = ['label' => 'Users', 'url' => ['/user/index']];

    $menuItems[] = [

        'label' => 'Logout (' . Yii::$app->user->identity->username . ')',

        'url' => ['/site/logout'],

        'linkOptions' => ['data-method' => 'post']



If you are using RBAC, in you if statement you would write RBAC check like this:

// if user can manage users, display him Users menu item

if (Yii::$app->user->can('manageUsers')


    $menuItems[] = ['label' => 'Users', 'url' => ['/user/index']];


// other code that you may need

Thank you so much it really helps :)