Using different layouts


I know that I can set the layout in each controller like this: $this->layout = ‘mylayout’;

That works for the controllers that I control/develop, but let’s say that I use an extension like SRBAC, and have a admin layout that I would like to use, how can I do that ?

In other words, how can I have different layouts for the frontend and the backend of an application?

Thanks in advance.

Kind regards


I do not understand the question exactly, but… SRBAC is not an extension… it is a module with adjustable views. Thus, you can control it (application.modules.srbac.views.layouts). You also may want to customize the css file supplied with the RBAC… it is placed under <application>/modules/srbac/css

"how can I have different layouts for the frontend and the backend of an application?"

Simple, creatimg different layouts under "layouts" folder… either for an application or for a certain module.

Well, IMHO the question is quite simple :)

SRBAC was just an example of something ‘external’ and I don’t want to modify any files from external modules or extensions as it will just add extra effort when the module/extension get updated.

So you suggest that i can create a layout called srbac.php in the layouts folder and that will work… I don’t think it will :)

I might not have been clear enough. If a controller don’t set a specific layout, /protected/views/layouts/main.php is used.

I have created a new controller for the backend that uses the layout that I want, but if I call modules/extensions like SRBAC, it uses the default (/protected/views/layouts/main.php) and I would like it to use the backend.php

I hope that makes my question a little more clear.

Kind regards


I understand now :)) And it looks like an issue for me too… I’ve created admin module (shell cli “module admin”) to separate front-end from the back-end… So, now I have index.php?r=admin/controller/action. For example we want to manage Users (that what I’ve done already for my application)… So, I’ve created UsersController under modules/admin/controllers and described appropriated actions… For example I want to view a list of users of my website in admin module… I just copied my code from application.controllers.UserController list action to application.modules.admin.controllers.UsersController and now I only need to change render function’s first attribute in this way:


Beforehand, in AdminModule.php init() method I’ve defined $this->layout = ‘main’ that means that I will use application.modules.admin.views.layouts.main (our back-end)

So, now, by index.php?r=admin/users/list I have a back-end for users list.

So, the promlem that Yii in its "nature" does not differentiate front-end and back-end… In a particular controller we have all functionality, but logically you may want to separate your site into the two parts (admin that is represents back-end and front-end part - what we do show to web users). In this sense it is more logical to pick out admin functions into the module. Of cource, it takes some efforts, but this way seemed to be the most suitable. Other thoughts?

Does it resolves your problem? Or I didn’t get what you need again? ))

I have found this :Cookbook sample, but that don’t solve the issue regarding ‘external’ modules/extensions.

It seems like we have hit one of the limitations of Yii :(

I dont think so. For example you have an extension…

We do not take in consideration an ApplicationComponent

Widget has it’s own view.

Action has not a view by it’s definition… Filter and Validator likewise Action do not imply to have a view.

In Controller extension you are free to chose layout. For example, you have downloaded CController extension (CExtController… whatever), you can specify your layout either in application components (is you want to use it as a component) or right inside the Controller.

Module… do whatever you want.

Yii is very flexible ;)

but still you can’t have a backend and a frontend with different layouts… unless you have to do a lot extra work.

I’ll find another solution… or framework

Could you let me know, if you will find the better solution?

I’ve migrated to Yii from native-php :)) So far I like it though of this issue. Maybe it does not seemed to me to be a really significant problem to do all this work…

Yii still rocks, but maybe, viewing this topic, Quiang will decide to add some extras to Yii 1.1 to separate front-end and back-end more easily (but how? Yii supply plenty of tools). In this case, maybe we could invent how should it be to submit it for a consideration?..

I do this kind of thing in a BaseController. Something like:

class BaseController extends CController {

    public function init() {






All controllers extend BaseController.

Presumably you’d determine the required layout based on Yii::app()->request->requestUri or


Yes, but that still involves modifying either the core framework or 3rd party modules/extensions which is what I would like to avoid in order to be able to upgrade as easy as possible - read without modifying core/3rd party to fit into my app.

It’s kind of strange/funny that nobody has had this problem before as i would say that it’s pretty common for a web app that it have a frontend with one layout/functionality and a backend with different layout/functionality

Okay, I understand. Basically, we need a hook between Yii creating the WebApplication and actually doing something with our controllers and modules. The only place I think we can do that is in index.php. So, how about replacing:


with something like:

  $webapp = Yii::createWebApplication($config);

  $webapp->onBeginRequest = array(new SomeClass, 'setLayout');


That is, attaching an event handler onto onBeginRequest. That doesn’t require changes elsewhere, and it works in modules too.