2013-02-18 208 views
0

我正在嘗試在Yii的子模塊中註冊事件。如何在Yii的模塊中註冊事件?

它似乎並不工作。

init方法絕對是所謂的。

class TestModule extends CWebModule 
{ 
    public function init() 
    { 
     $this->setImport(array(
      'test.models.*', 
      'test.components.*', 
     )); 
     Yii::app()->onBeginRequest = array($this, 'onBeginRequest'); 
    } 

    public function onBeginRequest($event) { 
     die('Request!'); 
    } 

    public function beforeControllerAction($controller, $action) 
    { 
     if (parent::beforeControllerAction($controller, $action)) 
     { 
      return true; 
     } 
     else 
      return false; 
    } 

} 
+0

您嘗試返回die('Request!'); ? – FabianoLothor 2013-02-18 12:56:10

回答

0

我解決了自己的問題。

問題是,我實際上爲onBeginRequest太晚了(Request is alrdy processed)。

所以我所做的是用onBeginRequest和onEndRequest事件處理程序編寫組件,在config/main.php中註冊事件處理程序並從此組件調用我的模塊。我基本上不得不代理所有這些事件。

1

要註冊一個事件,你可以這樣做:

$this->getEventHandlers($eventName)->add($eventHandler); 

哪裏$eventHandler是要定義爲$eventName事件回調的名稱。

您也可以通過以下方式做到這一點:

$this->attachEventHandler($eventName, $eventHandler); 
+0

這是註冊事件的權利,但沒有解決我的問題。 – 2013-03-08 12:34:18