Switch Yii layout based on user role

You can assign different layouts to different users using following filter class

1. Add LayoutFilter.php to your protected/components/ folder

class LayoutFilter extends CFilter
{
 
    protected function preFilter($filterChain)
    {
        // logic being applied before the action is executed
        $roles = Yii::app()->user->getState('roles');
        if (!is_array($roles))
            $roles = array();
 
        if (in_array(User::ROLE_ADMIN, $roles))
        {
            $filterChain->controller->layout = 'admin/column2';
        } else
        {
            $filterChain->controller->layout = '//layouts/column2';
        }
 
        return parent::preFilter($filterChain);
    }
 
    protected function postFilter($filterChain)
    {
    // logic being applied after the action is executed
        return parent::postFilter($filterChain);
    }
 
}

2. Update protected/components/Controller.php and add following filters

public function filters()
{
    return array(
        'rights',
        array('application.components.LayoutFilter'),
    );
}

3. Update all controllers and add following filters

public function filters()
{
    return parent::filters() + array(
        'postOnly + delete', // we only allow deletion via POST request
    );
}

Author: Tahir Yasin

Tahir is a Passionate Web Developer from Lahore, Pakistan and a guy with NEVER-STOP-LEARNING attitude who keeps an eye on emerging technologies and customer trends.

18 thoughts on “Switch Yii layout based on user role”

  1. i m using only one login page, i want to login user and admin. when user login change userlayout and when admin login change adminlayout. otherwise i dont no need front and back panels.
    Pls help me.

  2. You can have a role column in your users table, and set that role to the user at login time in UserIdentity.php like

    Yii::app()->user->setState(‘role’, $user->role);

    And you can check that role at any time anywhere in the application, on the basis of user role you can easily switch layout in any action of any controller by

    if(Yii::app()->user->getState(‘role’) == ‘admin’)
    {
    $this->layout = ‘//layouts/column2’;
    }

    1. This line of coding not working for me sir. im using yii rights and yii user extensions..so cannot work.

      shall i send my project..pls help me,

      Thanxs for your valuable time for me sir,

      1. I got the solution..I m very very Happy sir..

        ————————————————————————————
        //Login Controller
        ————————————————————————————
        if (Yii::app()->user->isSuperuser)
        //some actions here ..
        $this->redirect(array(‘/mailbox’));
        else
        $this->redirect(Yii::app()->user->returnUrl);

        ————————————————————————————
        //particular module view control..i.e., mailbox controller
        if (Yii::app()->user->isSuperuser)
        {
        $this->layout=’//layouts/colomn1′;
        }
        else
        {
        $this->layout=’//layouts/colomn2′;
        }

        ————————————————————————————–

  3. Can anyone teach me how to write a HTML layout (Howrse.com compatible) or redirect me to a website that has super basic instructions?

    Note: any HTML layout format type will work, but to see what I’m lookiing for, go to a Howrse player’s page to see a layout.

    Thanks all.

Leave a Reply

Your email address will not be published. Required fields are marked *