You can assign different layouts to different users using following filter class1. 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 ); }
Yii
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 [code language=
· 5 min read
Related posts
Yii
How to separate front and admin panel in yii-framework
In my previous post I demonstrated you how to render different layout for different user role, now lets move a step forward and see how to create separate front
Yii
How to save multiple related models in Yii [Complete Solution]
Scenario: You are in a situation where you have two related tables a Parent and a child table. You need to create a user experience in which user presses Save b
Yii
How to make Yii checkBoxList selected
Many people find it hard to make the Yii checkBoxList selected on update view, but believe me in fact its very easy and straight forward. This post will help yo