繁体   English   中英

FOSUserBundle,EventListener注册用户

[英]FOSUserBundle, EventListener registration user

我正在FOSUserBundle上,在RegistrationListener的EventListener上。

在此捆绑软件中,当我创建用户时,我使用方法updateUser()(在Vendor ... 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));
    }

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

步骤1:选择以下事件之一以侦听(取决于您何时捕获该过程)

/**
     * 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';

步骤2优先实现事件订阅服务器

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

步骤3实现您的功能

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

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

在这种情况下,您永远都不应修改第三方库,而要使用Event Dispatcher System来更早地处理事件,并且在需要时停止传播并避免事件的“重新处理”。

希望能帮助到你!!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM