class TestController extends \yii\web\Controller {
public function processEventHuyPV($event) {
$model = $event->sender; #\app\models\HuypvBook (id=1)
$data = $event->data; #'^^ is optional data'
echo 'Received event ' . $event->name . '. Now will process '. get_class($model).' ID=' .$model->id . ', include data=' . $data . '
';
# Process model
# ...
}
public function actionEvt() {
$this->layout = false;
$model = \app\models\HuypvBook::findOne(1);
# Register event
$model->on('my_event_name_is_huypv', [$this, 'processEventHuyPV'], '^^ is optional data');
# Do something or do nothing
for ($i = 0; $i < 10; $i++) {
#...
}
# Call to process event
$model->trigger('my_event_name_is_huypv');
# Do something else
for ($i = 0; $i < 10; $i++) {
#....
}
return 'Done';
}
}