簡體   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