1

EasyAdminBundle 使用 FOSUserBundle 用户操纵器来创建和管理用户。我想捕获创建用户以更新后缀数据库时使用的密码。创建用户时,开发分析器不会显示已侦听任何 FOSUserBundle 事件。唯一的 EasyAdminBundle 事件是easy_admin.post_initialize. 班级EasyAdminEvents展示

/** @Event("Symfony\Component\EventDispatcher\GenericEvent") */
const POST_INITIALIZE = 'easy_admin.post_initialize';

但是一个事件监听器:

use Symfony\Component\EventDispatcher\GenericEvent;
...
    public static function getSubscribedEvents()
    {
        return array(
            GenericEvent::POST_INITIALIZE => 'onPostInitialize',
        );
    }

抛出:

致命错误:未捕获的 Symfony\Component\Debug\Exception\FatalThrowableError:未定义的类常量 'POST_INITIALIZE' ...

我认为答案是使用JavierEguiluz\Bundle\EasyAdminBundle\Event\EasyAdminEvents事件而不是 Symfony GenericEvent。如在

use JavierEguiluz\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
...
    public static function getSubscribedEvents()
    {
        return array(
            EasyAdminEvents::POST_INITIALIZE => 'onPostInitialize',
        );
    }
...
//error occurs on the following line:
    public function onPostInitialize(EasyAdminEvents $event)
    {
        ...
    }

但是,这样做给了我这个 Catch 22:

必须是 JavierEguiluz\Bundle\EasyAdminBundle\Event\EasyAdminEvents 的实例,给定 Symfony\Component\EventDispatcher\GenericEvent 的实例”

4

1 回答 1

0

所以最简单的答案是更改类型提示,如下所示:

    public function onPostInitialize(GenericEvent $event)
    {
        ...
    }
于 2016-11-05T14:50:30.343 回答