EasyAdminBundle 事件 POST_INITIALIZE 未定义 class 常量

EasyAdminBundle event POST_INITIALIZE is Undefined class constant

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

/** @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',
        );
    }

投掷:

Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Undefined class constant '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)
    {
        ...
    }

但是,这样做给了我第 22 条军规:

must be an instance of JavierEguiluz\Bundle\EasyAdminBundle\Event\EasyAdminEvents, instance of Symfony\Component\EventDispatcher\GenericEvent given"

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

    public function onPostInitialize(GenericEvent $event)
    {
        ...
    }