我正在努力获取CakePHP(v3.x)事件工作中的最终链接。在我的Controler add
方法我有公共职能CakePHP添加事件监听器
add()
{
$event = new Event('Model.Comment.created', $this, [
'comment' => $comment
]);
$this->eventManager()->dispatch($event);
}
,并有我的监听器类设置:
namespace App\Event;
use Cake\Log\Log;
use Cake\Event\EventListener;
class CommentListener implements EventListener {
public function implementedEvents() {
return array(
'Model.Comment.created' => 'updatePostLog',
);
}
public function updatePostLog($event, $entity, $options) {
Log::write(
'info',
'A new comment was published with id: ' . $event->data['id']);
}
}
,但不能得到听者设置正确,特别是与我的应用程序知道我CommentListener
类存在。
它是否显示一些错误或警告? –
不,运行,但我没有做任何事情,我知道我错过了将两者联系在一起的那一点,我不确定它是如何实现的。 –
看文档: http://book.cakephp.org/3.0/en/core-libraries/events.html#registering-listeners 我很困惑这些行的去向: //附加UserStatistic对象订单的活动经理 $ statistics = new UserStatistic(); $ this-> Orders-> eventManager() - > on($ statistics); –