---
title: "YII基础知识学习一：yii布局模块modules"
author: "yd514"
date: "2013-08-29T16:30:28Z"
tags: []
categories: ["Web前端"]
slug: "yii-yii-modules"
draft: false
lastmod: "2013-08-29T16:31:55Z"
id: 1933392
url: "https://yd514.iteye.com/blog/1933392"
---

PS，本人新手，纯学习笔记总结，欢迎共同学习讨论。

一、gii自动生成布局模块

1&gt; 在config中main.php中开启gii，即：把下面的注释去掉

'modules'=&gt;array(  
        // uncomment the following to enable the Gii tool  
        /*  
        'gii'=&gt;array(  
            'class'=&gt;'system.gii.GiiModule',  
            'password'=&gt;'Enter Your Password Here',  //此处设置2的gii密码  
            // If removed, Gii defaults to localhost only. Edit carefully to taste.  
            'ipFilters'=&gt;array('127.0.0.1','::1'),  
        ),  
        \*/  
    ),

2&gt; 输入gii地址(如：shop项目，http://localhost/shop/index.php?r=gii/default/login)，进入gii密码界面；

a,输入密码（上面“1&gt;”设置好的）；  
b,进入页面，选择最后一个布局创建（Module Generator）；

c,然后在Module ID的输入框中，输入你的模块名称，如：houtai 再点击Preview 继续点击Generate；再回protected查看是否存在modules/houtai文件，存在就OK完成自动创建gii的houtai模块了。

3&gt; 开启模块调用功能，把houtai加入main.php的modules中，即，在modules中加入"houtai"元素值；如下：  
'modules'=&gt;array(  
        // uncomment the following to enable the Gii tool          
        'gii'=&gt;array(  
            'class'=&gt;'system.gii.GiiModule',  
            'password'=&gt;'123321',  
            // If removed, Gii defaults to localhost only. Edit carefully to taste.  
            'ipFilters'=&gt;array('127.0.0.1','::1'),  
        ),  
                'houtai',  
    ),

二、后台layout文件的调用与设置

1&gt; 由于shop项目（下方统称为前端页），也有一个对应的layout；即:你的实际盘根路径\\shop\\protected\\components\\Controller.php和你的实际盘根路径\\shop\\protected\\views\\layouts\\column1.php两个调用和设置文件了。在默认状态下，gii创建的houtai（下方统称后台）如果用render开启布局渲染的话，会直接把前端页的父类布局内容调出来。当然这不是我们要的结果!

所以，我们再设置一下，让后台调用新的布局。

设置方法：

1)直接复制前端布局的相应2个文件，粘进houtai对应文件夹，文件夹名肯定要自己新增的，效果如下：  
a&gt; houtai\\components\\Controller.php  
b&gt; 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';

三、column1.php设置

跟模板的替换原则一样，用$content做变量，加载页面时，换成render渲染的内容。  
注：个人建议，不要把局部重用的页面内容（如：产品左侧类别块）也想丢进这个页面，你可以直接require\_once包含进去，效果相差不大的。