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 ); }
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=
Related posts
Yii
Remove index.php from URL in YII
Learn how to configure Yii's UrlManager and Apache .htaccess rules to create clean, user-friendly URLs. This guide covers URL configuration, rewrite rules, common issues, and practical tips for improving Yii application routing
Yii
Yii Rights: Get all assigned roles of a user
Learn how to retrieve user roles in Yii Framework using the authassignment table, CDbCommand, and queryAll(). This guide explains how to fetch and store user roles, use parameter binding for safer SQL queries, leverage Yii’s built-in authorization manager, and implement role-based access control in PHP applications.
Yii
Disabled Yii logs and stack trace messages
Open up the index.php from root folder and comment out following 2 lines [code language=”php”] defined(‘YII_DEBUG’) or define(‘YII