class Comment
{
const NAME = 'comment';
const NAME_PUBLISH = 'comment:publish';
}
class CommentListener extends Plugin
{
public function publish(Event $event)
{
}
}
/**
* Class CommentController
* @package app\controllers
*/
class CommentController extends BaseController
{
public function initialize()
{
//监听事件
$this->eventsManager->attach(Comment::NAME, new CommentListener());
}
/**
* 发表一条评价
* @return string
*/
public function publishAction()
{
$post = $this->request->getPost();
if ($this->db->insertAsDict('comments', $post) {
$this->eventsManager->fire(Comment::NAME_PUBLISH, null);//扫行事件
return 0;
}
return 500;
}
}