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

1、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',
    ),
2、后台layout文件的调用与设置
1> 因为shop项目(下方统称为前端页),也有一个对应的layout;即:你的实际盘根路径\shop\protected\components\Controller.php和你的实际盘根路径\shop\protected\views\layouts\column1.php两个调用和设置文件了。在默认状态下,gii建立的houtai(下方统称后台)若是用render开启布局渲染的话,会直接把前端页的父类布局内容调出来。固然这不是咱们要的结果!

因此,咱们再设置一下,让后台调用新的布局。
设置方法:
1)直接复制前端布局的相应2个文件,粘进houtai对应文件夹,文件夹名确定要本身新增的,效果以下:
a> houtai\components\Controller.php
b> houtai\views\layouts\column1.php(column1.php可任意换名,如xx.php)

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

相关文章
相关标签/搜索