简体   繁体   English

FOSUserBundle,EventListener注册用户

[英]FOSUserBundle, EventListener registration user

I'm working on the FOSUserBundle, on EventListener for RegistrationUser. 我正在FOSUserBundle上,在RegistrationListener的EventListener上。

In this bundle, when I create a user, I use a method updateUser() (in Vendor...Model/UserManagerInterface). 在此捆绑软件中,当我创建用户时,我使用方法updateUser()(在Vendor ... Model / UserManagerInterface中)。 This method seems to be subject to an EventListener that triggers at least two actions. 此方法似乎受触发至少两个动作的EventListener的约束。 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 :-) 感谢您的关注和帮助:-)

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. 推荐的方法如下。

Step 1: Select one of the following events to listen(depending on when you want to catch the process) 步骤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';

Step 2 Implement the Event Subscriber with a priority 步骤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
            ],
        );
    }

Step 3 Implement your function 步骤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));
    }

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

Hope it helps!!!! 希望能帮助到你!!!!

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

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