骑驴找蚂蚁

全干工程师

在phalcon中使用自定义事件

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;
    }
}

留言