简体   繁体   中英

Symfony2 - Trying to understand FOSUserBundle Registration confirmation

I am new to Symfony and I pretty much now understand the basics and now I am diving into Events and Events Listeners.

I understand that when a user registers via FOSUserBundle there are three Events that get dispatched inside registerAction()

  1. REGISTRATION_INITIALIZE
  2. REGISTRATION_SUCCESS
  3. REGISTRATION_COMPLETED

This is the registerAction code

public function registerAction(Request $request)
    {
        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->get('fos_user.registration.form.factory');
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');
        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->get('event_dispatcher');

        $user = $userManager->createUser();
        $user->setEnabled(true);

        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

        if (null !== $event->getResponse()) {
            return $event->getResponse();
        }

        $form = $formFactory->createForm();
        $form->setData($user);

        $form->handleRequest($request);

        if ($form->isValid()) {
            $event = new FormEvent($form, $request);
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

            $userManager->updateUser($user);

            if (null === $response = $event->getResponse()) {
                $url = $this->generateUrl('fos_user_registration_confirmed');
                $response = new RedirectResponse($url);
            }

            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

            return $response;
        }

        return $this->render('FOSUserBundle:Registration:register.html.twig', array(
            'form' => $form->createView(),
        ));
    }

Now inside the config.yml we can setup the configuration so the user is required to confirm their email address before their account gets activated and this is where I get lost

fos_user:
    registration:
        confirmation:
            enabled: true

I am looking at EmailConfirmationListener which listens to the REGISTRATION_SUCCESS and inside this code I am finding nothing that tells me how it is reading the confirmation in config.yml

I will really appreciate if someone can explain to me how this listener is triggered based on confirmation enabled status.

The symfony doc explains

In order to load service configuration, you have to create a Dependency Injection (DI) Extension for your bundle

I did a search in the repository and, as you can see , the FOSUserExtension.php file loads a service file which contains the listener declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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