Symfony2 - 试图了解 FOSUserBundle 注册确认
Symfony2 - Trying to understand FOSUserBundle Registration confirmation
我是 Symfony 的新手,现在我几乎了解了基础知识,现在我正在深入研究事件和事件监听器。
我了解到,当用户通过 FOSUserBundle 注册时,会在 registerAction()
中调度三个事件
- REGISTRATION_INITIALIZE
- REGISTRATION_SUCCESS
- REGISTRATION_COMPLETED
这是registerAction
代码
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(),
));
}
现在在 config.yml
中,我们可以设置配置,这样用户在激活帐户之前需要确认他们的电子邮件地址,这就是我迷路的地方
fos_user:
registration:
confirmation:
enabled: true
我正在查看 EmailConfirmationListener,它听取 REGISTRATION_SUCCESS
并且在这段代码中我没有发现任何东西可以告诉我它是如何读取 config.yml
中的 confirmation
如果有人能向我解释如何根据 confirmation
enabled
状态触发此侦听器,我将不胜感激。
In order to load service configuration, you have to create a
Dependency Injection (DI) Extension for your bundle
我在存储库中进行了搜索,you can see,FOSUserExtension.php
文件加载了一个包含侦听器声明的服务文件。
我是 Symfony 的新手,现在我几乎了解了基础知识,现在我正在深入研究事件和事件监听器。
我了解到,当用户通过 FOSUserBundle 注册时,会在 registerAction()
- REGISTRATION_INITIALIZE
- REGISTRATION_SUCCESS
- REGISTRATION_COMPLETED
这是registerAction
代码
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(),
));
}
现在在 config.yml
中,我们可以设置配置,这样用户在激活帐户之前需要确认他们的电子邮件地址,这就是我迷路的地方
fos_user:
registration:
confirmation:
enabled: true
我正在查看 EmailConfirmationListener,它听取 REGISTRATION_SUCCESS
并且在这段代码中我没有发现任何东西可以告诉我它是如何读取 config.yml
中的 confirmation
如果有人能向我解释如何根据 confirmation
enabled
状态触发此侦听器,我将不胜感激。
In order to load service configuration, you have to create a Dependency Injection (DI) Extension for your bundle
我在存储库中进行了搜索,you can see,FOSUserExtension.php
文件加载了一个包含侦听器声明的服务文件。