미안하지만 난 여기에 "자동 후크"에 대해 알고 있지만하지 않는이 :
이 아무것도 특별하지하지 않는 테스트 클래스, 그냥 함수 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
을 사용하고 전달할 매개 변수를 그것을 할 수 있습니다 .
documentation/wiki를 보시지 않으시겠습니까? http://www.yiiframework.com/wiki/327/events-explained/ – Latheesan
내 질문에 대한 답변이없는 문서에 대한 2 개의 링크를 이미 게시했습니다. 하지만 고마워요. 나는 이것도 살펴볼 것입니다. – Jurik
오케이. - 내가 쓴 것처럼 당신의 링크에 관해서는 불가능합니다. - '마법의'청취자가 없으며, 당신 스스로 당신의 사건을 제기해야합니다. – Jurik