public function __construct($id,$parent,$config=null)//$id为模块ID,$parent为父模块ID { $this->_id=$id; $this->_parentModule=$parent; // set basePath at early as possible to avoid trouble if(is_string($config)) $config=require($config); if(isset($config['basePath'])) { $this->setBasePath($config['basePath']);//设置模块路径 unset($config['basePath']); } Yii::setPathOfAlias($id,$this->getBasePath());//把该模块的路径添加到Alias中,方便访问 //下面几个函数就是模块的初始化需要做的几件事情,可以通过重写preinit()和init()来自定义模块可以通过自定义相应的函数来定制和初始化模块的参数 $this->preinit();//预初始化,一般用来设置模块相关的行为对象$behaviors[],预加载对象$preload[],和其他配置。 $this->configure($config);//加载配置 $this->attachBehaviors($this->behaviors);//挂载行为对象 $this->preloadComponents();//加载CModule::$preload[]中的预加载组件 $this->init();//模块真正初始化,一般情况下,重写该函数完整模块初始化 }
CWebModule分析
public function beforeControllerAction($controller,$action) { if(($parent=$this->getParentModule())===null) $parent=Yii::app(); return $parent->beforeControllerAction($controller,$action); } public function afterControllerAction($controller,$action) { if(($parent=$this->getParentModule())===null) $parent=Yii::app(); $parent->afterControllerAction($controller,$action); } 可以通过此两个函数对该模块中的每一个请求进行一些改写,控制以及收尾的相关工作。在自定义的模块中,两个函数的覆盖的模版如下: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; }