douyonglang4845 2016-05-04 12:07
浏览 25
已采纳

Symfony2 - 尝试了解FOSUserBundle注册确认

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.

  • 写回答

1条回答 默认 最新

  • doulan8330 2016-05-04 13:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗