2014-05-13 1 views
1

후크 시스템과 Yii의 이벤트 처리 시스템이 같은지 확실하지 않습니다.Yii 이벤트 처리는 어떻게 작동합니까?

문서는 thisthis 문서에 대해 클래스에 이벤트를 첨부해야하며 코드의 어딘가에이 이벤트를 제기해야합니다.

하지만 내 이해 후크 시스템은 다음과 같이 작동

class foo {  
    public function bar() 
    { 
    //do something 
    }  
} 

그리고 지금 당신이 어딘가에 정의 함수가 beforeFooBar($obj) 또는 afterFooBar($obj)라고 (프레임 워크에 따라 다름)이 기능은 이전 또는 함수 줄 이후에()를 호출 실행됩니다.

Yii와 비슷한 것이 있습니까? 아니면 후크 시스템에 대한 나의 이해가 완전히 잘못된 것입니까?

편집 :
내가 beforeSave()afterSave() 유사한 기능을 요구하고있다. 그 사건 때문에 당신은 기상 할 필요가 없습니다. YII에서 이벤트 처리가 어떻게 작동하는지

+0

documentation/wiki를 보시지 않으시겠습니까? http://www.yiiframework.com/wiki/327/events-explained/ – Latheesan

+0

내 질문에 대한 답변이없는 문서에 대한 2 개의 링크를 이미 게시했습니다. 하지만 고마워요. 나는 이것도 살펴볼 것입니다. – Jurik

+0

오케이. - 내가 쓴 것처럼 당신의 링크에 관해서는 불가능합니다. - '마법의'청취자가 없으며, 당신 스스로 당신의 사건을 제기해야합니다. – Jurik

답변

1

미안하지만 난 여기에 "자동 후크"에 대해 알고 있지만하지 않는이 :

이 아무것도 특별하지하지 않는 테스트 클래스, 그냥 함수 test()이있는 텍스트를 출력합니다. 그것은 모델, 구성 요소 또는 무엇이든 같이, 아무 것도 될 수있다 : 방법에

class TestClass extends CComponent 
{ 
    public function test() 
    { 
     echo 'Running test()<br />'; 

     echo 'Now firing event<br />'; 
     $event = new TestEvent($this, 1, 'someTestParam'); // see TestEvent class constructor 
     $this->onTest($event); 
    } 

    public function onTest($event) 
    { 
     $this->raiseEvent('onTest', $event); 
    } 
} 

우리는 이벤트 객체를 생성하고 이벤트를 발생시키는 우리의 방법 onTest()에 전달 test(). 이벤트 객체는 CEvent를 확장하는 간단한 클래스입니다. 우리는 이벤트 핸들러에 이벤트에 대한 정보를 전달하는 데 사용할 수 있습니다 :

class TestEvent extends CEvent 
{ 
    public $var1; 
    public $var2; 

    public function __construct(TestClass $sender, $var1, $var2) 
    { 
     parent::__construct($sender); 

     $this->var1 = $var1; 
     $this->var2 = $var2; 
    } 
} 

컨트롤러의 동작은 다음과 같습니다 : 당신이 그렇게 같은 이벤트를 발생시킬 수

public function actionEventTesting() 
{ 
    // create a new class 
    $class = new TestClass(); 

    // attach an event handler 
    $class->onTest[] = function($event) { 
     echo "Attached Event1 raised<br />"; 
     var_dump($event); 
    }; 

    // attach a 2nd event handler (note that this is another method of attaching as before) 
    $class->attachEventHandler('onTest', function($event) { 
     echo "Attached Event2 raised<br />"; 
     var_dump($event); 
    }); 

    // call our test method 
    $class->test(); 
} 

참고 :

class TestClass extends CComponent 
{ 
    public function test() 
    { 
     echo 'Running test()<br />'; 

     echo 'Now firing event<br />'; 
     $event = new TestEvent($this, 1, 'someTestParam'); 

     // this one is different as before 
     $this->raiseEvent('onTest', $event); 
    } 

    public function onTest($event) 
    { 
     // empty 
    } 
} 

왜 그런지 모르지만 그 비어있는 경우에도 onTest() 메서드가 필요합니다. 그렇지 않으면 이벤트 처리기를 객체에 첨부하려고 할 때 예외가 발생합니다. 하지만 이러한 "켜기"메서드를 추상 클래스 나 다른 것에 넣을 수 있으므로 귀찮게하지 않아도됩니다.

public function test() 
{ 
    echo 'Running test()<br />'; 

    echo 'Now firing event<br />'; 
    $event = new CEvent($this, array('var1' => 1, 'var2' => 'someTestParam')); 
    $this->onTest($event); 
} 

또한 행동에 이벤트를 선언 할 수 있지만 다른 이야기 :

또한 당신이 당신의 자신의 이벤트 클래스를 만들 필요가 없습니다 당신은 단순히 직접 CEvent을 사용하고 전달할 매개 변수를 그것을 할 수 있습니다 .

+0

오케이 - 이제 Yii 이벤트 처리가 더 잘 이해됩니다. 아마도 일반적인 경우는 CEvent 만 사용하고 모든 변수는'$ event-> params [ 'var1']'등입니다. – Jurik

관련 문제