FOSUserBundle、EventListener注册用户

FOSUserBundle, EventListener registration user

我正在研究 FOSUserBundle,在 RegistrationUser 的 EventListener 上。

在这个包中,当我创建一个用户时,我使用了一个方法 updateUser()(在供应商中...Model/UserManagerInterface)。此方法似乎受制于触发至少两个操作的 EventListener。注册输入数据库的信息。并向用户发送电子邮件以向他发送登录凭据。

我找到了发送邮件的方法。不利的是,我没有找到录音的人。我也没有找到在哪里设置这两个事件。

首先对于所有人(和我的个人信息),我试图找到这两点仍然未知。如果有人可以指导我?

然后,根据我们与客户的决定,我可能会继续收取附加费(我仍然不知道该怎么做),但我想一旦我的两个陌生人我会发现更好一点找到:-)

感谢您的关注和帮助:-)

这是处理注册成功的电子邮件确认的函数

FOS\UserBundle\EventListener\EmailConfirmationListener

public function onRegistrationSuccess(FormEvent $event)
    {
        /** @var $user \FOS\UserBundle\Model\UserInterface */
        $user = $event->getForm()->getData();

        $user->setEnabled(false);
        if (null === $user->getConfirmationToken()) {
            $user->setConfirmationToken($this->tokenGenerator->generateToken());
        }

        $this->mailer->sendConfirmationEmailMessage($user);

        $this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());

        $url = $this->router->generate('fos_user_registration_check_email');
        $event->setResponse(new RedirectResponse($url));
    }

但我告诉你,你正在尝试做的是一种不好的做法。推荐方式如下

Step 1: Select one of the following events to listen(depending on when you want to catch the process)

/**
     * The REGISTRATION_SUCCESS event occurs when the registration form is submitted successfully.
     *
     * This event allows you to set the response instead of using the default one.
     *
     * @Event("FOS\UserBundle\Event\FormEvent")
     */
    const REGISTRATION_SUCCESS = 'fos_user.registration.success';

/**
     * The REGISTRATION_COMPLETED event occurs after saving the user in the registration process.
     *
     * This event allows you to access the response which will be sent.
     *
     * @Event("FOS\UserBundle\Event\FilterUserResponseEvent")
     */
    const REGISTRATION_COMPLETED = 'fos_user.registration.completed';

Step 2 Implement the Event Subscriber with a priority

    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_SUCCESS => [
                'onRegistrationSuccess', 100 //The priority is higher than the FOSuser so it will be called first
            ],
        );
    }

Step 3 Implement your function

public function onRegistrationSuccess(FormEvent $event)
    {
       //do your logic here

        $event->stopPropagation();//the Fos User method shall never be called!!
        $event->setResponse(new RedirectResponse($url));
    }

在这种情况下,你永远不应该修改第三方库,事件调度系统就是为此而设计的,以便更早地处理事件,如果需要的话,停止传播并避免事件的"re-processing"。

希望对您有所帮助!!!!