YII前台和后台布局(转载)

一、gii自动生成布局模块
1> 在config中main.php中开启gii,即:把下面的注释去掉
'modules'=>array(
        // uncomment the following to enable the Gii tool
        /*
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'Enter Your Password Here',  //此处设置2的gii密码
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters'=>array('127.0.0.1','::1'),
        ),
        */
    ),
2> 输入gii地址(如:shop项目,http://localhost/shop/index.php?r=gii/default/login),进入gii密码界面;
a,输入密码(上面“1>”设置好的);
b,进入页面,选择最后一个布局创建(Module Generator);
c,然后在Module ID的输入框中,输入你的模块名称,如:houtai 再点击Preview 继续点击Generate;再回protected查看是否存在modules/houtai文件,存在就OK完成自动创建gii的houtai模块了。
3> 开启模块调用功能,把houtai加入main.php的modules中,即,在modules中加入"houtai"元素值;如下:
'modules'=>array(
        // uncomment the following to enable the Gii tool        
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'123321',
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters'=>array('127.0.0.1','::1'),
        ),
                'houtai',
    ),
二、后台layout文件的调用与设置
1> 由于shop项目(下方统称为前端页),也有一个对应的layout;即:你的实际盘根路径shopprotectedcomponentsController.php和你的实际盘根路径shopprotectedviewslayoutscolumn1.php两个调用和设置文件了。在默认状态下,gii创建的houtai(下方统称后台)如果用render开启布局渲染的话,会直接把前端页的父类布局内容调出来。当然这不是我们要的结果!

所以,我们再设置一下,让后台调用新的布局。
设置方法:
1)直接复制前端布局的相应2个文件,粘进houtai对应文件夹,文件夹名肯定要自己新增的,效果如下:
a> houtaicomponentsController.php
b> houtaiviewslayoutscolumn1.php(column1.php可任意换名,如xx.php)

2)修改houtaicomponentsController.php中,把
public $layout='//layouts/column1';中开头的双斜扛,去掉一个(因为://,则默认会加载protected/view/layouts/column1.php这个layout;),column1改成你自己新名字(xx.php);效果如下:
public $layout='/layouts/xx';
三、column1.php设置
跟模板的替换原则一样,用$content做变量,加载页面时,换成render渲染的内容。
注:个人建议,不要把局部重用的页面内容(如:产品左侧类别块)也想丢进这个页面,你可以直接require_once包含进去,效果相差不大的。

原文地址:https://www.cnblogs.com/feng003/p/3504639.html