different layouts

I’m implementing a password protected website. I have defined 2 different layouts in views/layout for guest and members. Basically header and footer are displayed for members and none for guest. I managed to get it work by defining function init() in controller classes:

public function init() {

    $this->layout = Yii::app()->user->isGuest ? 'guest'  : 'member';


It seems repetitive to duplicate this code in all controller classes. Is there a simpler way to define this only once and apply to all controllers?

put your code in components/Controller.php


Just a newbie with Yii, only started using it a week ago. Still have a lot to learn!

It’s more OOP than Yii :) Just put everything common for a set of classes in a parent class and then inherit.