How to change menus on sidebar of Admin LTE as per roles in yii2?

I am using Yii2 basic.

I have Employee table and I have assigned Employee model to user application component as follows in config file:

‘user’ => [

       'identityClass' => 'app\models\Employee',

       'enableSession' => true,


I have also created permissions, roles and assigned roles to employees using RBAC.

1. Now when employee whose role is admin logs in, he can see foll menus on sidebar of Admin LTE:

  1. Masters

  2. Employee

  3. Employee Training

  4. SHGProfile

  5. Survey

When employee whose role is fieldofficer logs in, he also sees above menus on sidebar. Fieldofficer should only see foll menus:

  1. My Profile

  2. SHGProfile

How to change the menus on sidebar of Admin LTE as per roles of the employees?

2. Employee should be able to log in to the system only if role is assigned to employee. How to accomplish this?

Just construct the menu items dynamically by checking the user’s permissions/roles.

$items = [];   // menu items

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

    $items = [

        ... // items for admins


} else if (Yii::$app->user->can('fieldofficer')) {

    $items = [

        ... // items for field officers


} else {

    $items = [

        ... // items for ordinary employees



I think you’ve got the things up-side-down. Any role except for ‘Guest’ can only be acquired after a successful login. In other words, an user who could successfully logged in to the system would get an Employee role or an Admin role or something like that according to your definitions.

So, what you have to think about is what actions should be permitted or prohibited to ‘Guest’.

Where to make changes in main.php?

This is my main.php file


/* @var $this \yii\web\View */

/* @var $content string */

use yii\helpers\Html;

use yii\widgets\Breadcrumbs;

use app\assets\DashboardAsset;

use app\models\Employee;



<?php $this->beginPage() ?>

<!DOCTYPE html>

<html lang="<?= Yii::$app->language ?>">


    <meta charset="<?= Yii::$app->charset ?>">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?= Html::csrfMetaTags() ?>

    <title><?= Html::encode($this->title) ?></title>

    <?php $this->head() ?>


<body class="skin-blue sidebar-mini">

<?php $this->beginBody() ?>

<div class="wrapper">

<header class="main-header">

    <!-- Logo -->

    <a href="#" class="logo">

      <!-- mini logo for sidebar mini 50x50 pixels -->


      <!-- logo for regular state and mobile devices -->

      <span class="logo-lg">Integrity Foundation</span>


    <!-- Header Navbar: style can be found in header.less -->

    <nav class="navbar navbar-static-top">

      <!-- Sidebar toggle button-->

      <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">

        <span class="sr-only">Toggle navigation</span>


      <div class="navbar-custom-menu">

        <ul class="nav navbar-nav">

          <!-- Messages: style can be found in dropdown.less-->


          <!-- Notifications: style can be found in dropdown.less -->


          <!-- Tasks: style can be found in dropdown.less -->


          <!-- User Account: style can be found in dropdown.less -->

          <li class="dropdown user user-menu">

            <a href="#" class="dropdown-toggle" data-toggle="dropdown">

			<?= Html::img('@web/uploadedimages/'.\Yii::$app->user->identity->Photo, ['class'=>"user-image", 'alt'=>"User Image"]) ?>


              <span class="hidden-xs"><?php echo \Yii::$app->user->identity->fullname;?></span>


            <ul class="dropdown-menu">

              <!-- User image -->

              <li class="user-header">

                <?= Html::img('@web/uploadedimages/'.\Yii::$app->user->identity->Photo, ['class'=>"user-image", 'alt'=>"User Image"]) ?>


                  <?php echo \Yii::$app->user->identity->fullname." - ";?>


					$emp = Employee::findOne(\Yii::$app->user->identity->id);

					$empjob= $emp->employeejobs;

					foreach($empjob as $empjobs)




					echo $empjobs['Job_Title'];






              <!-- Menu Body -->

              <li class="user-body">

                <div class="row">



                <!-- /.row -->


              <!-- Menu Footer-->

              <li class="user-footer">


                <div class="pull-right">

                  <?= Html::a(

                                    'Log out',


                                    ['data-method' => 'post', 'class' => 'btn btn-default btn-flat']

                                ) ?>





          <!-- Control Sidebar Toggle Button -->






  <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->

    <section class="sidebar">

      <!-- Sidebar user panel -->



      <!-- sidebar menu: : style can be found in sidebar.less -->

      <ul class="sidebar-menu">


        <li class="active treeview">

          <a href="index.php?r=dashboard"><i class="fa fa-dashboard"></i> <span>Dashboard</span></a>



        <li class="treeview">

          <a href="#">

            <i class="fa fa-laptop"></i>


            <span class="pull-right-container">

              <i class="fa fa-angle-left pull-right"></i>



          <ul class="treeview-menu">

            <li><a href="pages/UI/general.html"><i class="fa fa-circle-o"></i> Church</a></li>

			<li><a href="index.php?r=courses"><i class="fa fa-circle-o"></i> Courses</a></li>

            <li><a href="index.php?r=district"><i class="fa fa-circle-o"></i> District</a></li>

            <li><a href="index.php?r=taluka"><i class="fa fa-circle-o"></i> Taluka</a></li>

            <li><a href="index.php?r=village"><i class="fa fa-circle-o"></i> Village</a></li>

            <li><a href="index.php?r=location"><i class="fa fa-circle-o"></i> Locations</a></li>

            <li><a href="index.php?r=area"><i class="fa fa-circle-o"></i> Areas</a></li>





        <li><a href="index.php?r=employee"><i class="glyphicon glyphicon-user"></i>Employee</a></li>

		<li><a href="index.php?r=employeetraining"> <span class="glyphicons glyphicons-user">Employee Training</span></a></li>

		<li><a href="index.php?r=groupdetails"> <span class="glyphicons glyphicons-user">SHG Profile</span></a></li>

		<li class="treeview">

          <a href="#">

            <i class="fa fa-laptop"></i>


            <span class="pull-right-container">

              <i class="fa fa-angle-left pull-right"></i>



          <ul class="treeview-menu">

            <li><a href="index.php?r=villagedata"><i class="fa fa-circle-o"></i> Village Data</a></li>

			<li><a href="pages/UI/general.html"><i class="fa fa-circle-o"></i> Area Data</a></li>

            <li><a href="index.php?r=persondata"><i class="fa fa-circle-o"></i> Survey</a></li>





		<li><a href="documentation/index.html"> <span class="glyphicons glyphicons-user">Church Membership</span></a></li>

		<li><a href="index.php?r=dailytaskreport"> <span class="glyphicons glyphicons-user">Daily Task Reports</span></a></li>

				<li><a href="index.php?r=weeklymeetingreport"> <span class="glyphicons glyphicons-user">Weekly Meeting Reports</span></a></li>




    <!-- /.sidebar -->



    <div class="content-wrapper">

    <!-- Content Header (Page header) -->

    <section class="content-header">


      <!--<?php echo \Yii::$app->user->identity->FirstName; ?>!-->


    <!-- Main content -->

    <section class="content">

        <?=$content ?>





<?php $this->endBody() ?>



<?php $this->endPage() ?>

Where should I construct menu items dynamically in which file?

Something like the following in "main.php" layout file:

<?php if (Yii::$app->user->can('admin')): ?>

    <li><a href="..."> <span class="...">Employee Training</span></a></li>

    <li><a href="..."> <span class="...">SHG Profile</span></a></li>

    ... <!-- items for admins -->

<?php else if (Yii::$app->user->can('fieldofficer')): ?>

    <li><a href="..."> <span class="...">Something</span></a></li>

    <li><a href="..."> <span class="...">Other</span></a></li>

    ... <!-- items for field officers -->

<?php else: ?>

    ... <!-- items for employees -->

<?php endif ?>