CakePHP 3 - implementedEvents() - 不触发已实现的事件
CakePHP 3 - implementedEvents() - does not fire implemented event
尝试从 CakePHP 事件侦听器开始。我已经设置了一个事件,但它没有触发。我不明白为什么?这是我到目前为止的代码...
public function view($slug = null) {
$profile = $this->Profiles->find()->where(['slug' => $slug])->first();
$this->set('profile', $profile);
$this->set('_serialize', ['profile']);
}
// I have confirmed this works.. but it is not calling the updateCountEvent method
public function implementedEvents(){
$_events = parent::implementedEvents();
$events['Controller.afterView'] = 'updateCountEvent';
return array_merge($_events, $events);
}
/**
* Triggered when a profile is viewed...
*/
public function updateCountEvent(){
Log::write('error', "Update count events"); // I dont get this line in the log. Not sure why this does not fire...
}
我重新审视了这个问题,并提出了一个适合我的解决方案。感谢 Jose Lorenzo 'heads up'。这是我的解决方案:
use Cake\Event\Event;
public function view($slug = null) {
$profile = $this->Profiles->find()->where(['slug' => $slug])->first();
$this->profileId = $profile->id;
$event = new Event('Controller.Profiles.afterView', $this);
$this->eventManager()->dispatch($event);
$this->set('title', $profile->name);
$this->set('profile', $profile);
$this->set('_serialize', ['profile']);
}
public function implementedEvents(){
$_events = parent::implementedEvents();
$events['Controller.Profiles.afterView'] = 'updateCountEvent';
return array_merge($_events, $events);
}
public function updateCountEvent(){
$profile = $this->Profiles->get($this->profileId);
$profile->set('views_count', $profile->views_count + 1);
$this->Profiles->save($profile);
}
我看到了事件的力量,尤其是当我发送电子邮件、更新更多表格以及可能 运行 一个 cron...时,而不是编写这两行代码并创建另外 2 个方法对于这种特定情况,我本可以在 view
操作中进行这个简单的调用
$profile->set('views_count', $profile->views_count + 1);
$this->Profiles->save($profile);
问题是....我应该选择这个更简单的过程,还是坚持事件路线?
尝试从 CakePHP 事件侦听器开始。我已经设置了一个事件,但它没有触发。我不明白为什么?这是我到目前为止的代码...
public function view($slug = null) {
$profile = $this->Profiles->find()->where(['slug' => $slug])->first();
$this->set('profile', $profile);
$this->set('_serialize', ['profile']);
}
// I have confirmed this works.. but it is not calling the updateCountEvent method
public function implementedEvents(){
$_events = parent::implementedEvents();
$events['Controller.afterView'] = 'updateCountEvent';
return array_merge($_events, $events);
}
/**
* Triggered when a profile is viewed...
*/
public function updateCountEvent(){
Log::write('error', "Update count events"); // I dont get this line in the log. Not sure why this does not fire...
}
我重新审视了这个问题,并提出了一个适合我的解决方案。感谢 Jose Lorenzo 'heads up'。这是我的解决方案:
use Cake\Event\Event;
public function view($slug = null) {
$profile = $this->Profiles->find()->where(['slug' => $slug])->first();
$this->profileId = $profile->id;
$event = new Event('Controller.Profiles.afterView', $this);
$this->eventManager()->dispatch($event);
$this->set('title', $profile->name);
$this->set('profile', $profile);
$this->set('_serialize', ['profile']);
}
public function implementedEvents(){
$_events = parent::implementedEvents();
$events['Controller.Profiles.afterView'] = 'updateCountEvent';
return array_merge($_events, $events);
}
public function updateCountEvent(){
$profile = $this->Profiles->get($this->profileId);
$profile->set('views_count', $profile->views_count + 1);
$this->Profiles->save($profile);
}
我看到了事件的力量,尤其是当我发送电子邮件、更新更多表格以及可能 运行 一个 cron...时,而不是编写这两行代码并创建另外 2 个方法对于这种特定情况,我本可以在 view
操作中进行这个简单的调用
$profile->set('views_count', $profile->views_count + 1);
$this->Profiles->save($profile);
问题是....我应该选择这个更简单的过程,还是坚持事件路线?