module的目录结构
│ ├── models
│
│ ├──
ContactForm.php
│
│ ├──
LoginForm.php
│
│ └──
User.php................................................................
│ ├──
modules模块的存放目录
│
│ └──
testmod一个模块,模块的名字对应是目录的名字,唯一。也是路由中的moduleid
│
│
├── components模块用到的组件
│
│
├── controllers包含控制器
│
│
│ └──
DefaultController.php默认控制器
│
│
├── messages国际化
│
│
├── models模型类文件
│
│
├── TestmodModule.php模块的类文件
│
│
└── views试图文件
│
│
├──
default
默认视图
│
│
│ ├──
index.php视图文件
│
│
└── layouts包含布局文件
│ ├──
runtime....................................................................
│
│ └──
application.log
│ ├── tests
│
│ ├──
bootstrap.php
│
│ ├── fixtures
│
│
│ └── tbl_user.php
我的投票模块:模块必须继承CWebModule(->CModule->CComponent),类名是模块名首字母大写,后缀是Module
class
VoteModule
extends
CWebModule{
public
function
init()
{
//
this method is called when the module is being created
//
you may place code here to customize the module or the
application
//
import the module-level models and components
$
this
->setImport(array(
'vote.models.*'
,
'vote.components.*'
,
));
}
public
function
beforeControllerAction($controller,
$action)
{
if
(parent::beforeControllerAction($controller,
$action))
{
//
this method is called before any module controller action is
performed
//
you may place customized code here
return
true
;
}
else
return
false
;
}
public
function
afterControllerAction(){
if
(parent::afterControllerAction($controller,
$action))
{
//
this method is called after any module controller action is
performed
//
you may place customized code here
return
true
;
}
else
return
false
;
}
}
配置文件中也可以及添加对模块中属性初始化的参数例如:'modules'=>array('vote'=>array('param'=>'param1'), param为模块类的属性如下:配置文件main.php中添加
'modules'=>array('vote',),
class
VoteModule
extends
CWebModule
{
public
$param;
YII中的模块是非常灵活的,一个模块可以包含子模块,理论上,模块可以是无限嵌套。对应的访问方式是:Yii::app()->controller->module->param;
模块中的控制器动作通过
r=moduleID/controllerID/actionID进行访问