FOSUserBundle,EventListener注册用户 [英] FOSUserBundle, EventListener registration user

查看:86
本文介绍了FOSUserBundle,EventListener注册用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FOSUserBundle,使用的是EventListener的RegistrationUser.

I'm working on the FOSUserBundle, on EventListener for RegistrationUser.

在此捆绑包中,当我创建用户时,我使用了updateUser()方法(在Vendor ... Model/UserManagerInterface中).此方法似乎受触发至少两个动作的EventListener的约束.注册在数据库中输入的信息.然后向用户发送电子邮件,以向他发送登录凭据.

In this bundle, when I create a user, I use a method updateUser() (in Vendor...Model/UserManagerInterface). This method seems to be subject to an EventListener that triggers at least two actions. Registering the information entered in the database. And sending an email to the user to send him login credentials.

我找到了发送邮件的方法.出于不利因素,我没有找到进行录音的人.我也没有找到在哪里设置这两个事件.

I found the method that send the mail. By cons, I didn't find the one who makes the recording. I also didn't find where to set the two events.

首先,首先(以及我的个人信息),我试图找到这两点仍然未知.如果有人可以引导我?

First for all (and my personnal information), I try to find these two points still unknown. If anyone could guide me?

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

Then, depending on what we decide with our client, I may proceed to a surcharge (wich I still don't really know how to do), bu I imagine that I would find a little better once my two strangers found :-)

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

Thank's for your attention and help :-)

推荐答案

此功能可处理registrationSucces上的电子邮件确认

This is the function wich handles the email confirmation on registrationSucces

FOS \ UserBundle \ EventListener \ EmailConfirmationListener

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

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

But I tell you that what you are trying to do is a bad practice. The recommended way is the following.

第1步:选择以下事件之一进行监听(取决于您希望捕获该过程的时间)

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

第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步实现您的功能

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

在这种情况下,您绝对不要修改第三方库,因为它是由Event Dispatcher System组成的,以便它可以更早地处理事件,并且在需要时停止传播并避免事件的重新处理".

You never should modify the third party libraries in this case the Event Dispatcher System is made for this to earlier process the event and if its needed stop the propagation and avoid the "re-processing" of the event.

希望有帮助!!!!

这篇关于FOSUserBundle,EventListener注册用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆