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; }