2014-07-20 69 views
0

我需要一點幫助,因爲我的代碼不起作用,我找不到我失敗的地方。看,這就是我使用我的應用程序註冊用戶的功能(從here抽放和修改一下適合我的需要):REGISTRATION_SUCCESS事件沒有在FOSUserBundle上觸發

public function registerAction(Request $request) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */ 
    $userManager = $this->container->get('fos_user.user_manager'); 
    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */ 
    $dispatcher = $this->container->get('event_dispatcher'); 

    $user = $userManager->createUser(); 

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

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

    $entity = new SysUsuario(); 
    $form = $this->createForm(new UsuarioType(), $entity); 
    $form->handleRequest($request); 

    $user_data = $request->get('user_register'); 
    $profile_data = $request->get('user_register')['perfil']; 

    if ($form->isValid()) { 
     $user->setUsername($profile_data['persJuridica'] . $profile_data['rif']); 
     $user->setEmail($user_data['email']); 
     $user->setPlainPassword($user_data['password']); 

     $role = $profile_data['roleType']; 
     if ($role === "O") { 
      $user->addRole("ROLE_OPERADOR"); 
     } 
     elseif ($role === "CH") { 
      $user->addRole("ROLE_CENTRO_HIPICO"); 
     } 

     $userManager->updateUser($user); 

     $profile = new SysPerfil(); 
     $profile->setPersJuridica($profile_data['persJuridica']); 
     $profile->setRif($profile_data['rif']); 

     $ci = isset($profile_data['ci']) ? $profile_data['ci'] : null; 
     if ($ci != NULL) { 
      $profile->setCi($profile_data['ci']); 
     } 

     $profile->setNombre($profile_data['nombre']); 
     $profile->setApellido($profile_data['apellido']); 
     $profile->setRoleType($profile_data['roleType']); 
     $profile->setUser($user); 

     $em->persist($profile); 
     $em->flush(); 

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

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

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

     return $response; 
    } 
    else { 
     $errors = $this->getFormErrors($formProfile); 
    } 

    return new JsonResponse(array('status' => true, 'errors' => $errors)); 
} 

這裏的問題是,我的代碼永遠不會觸發REGISTRATION_SUCCESS事件,因此我沒有收到任何電子郵件,因此我無法確認用戶,我做錯了什麼?

+0

你重寫RegistrationController嗎? registerAction()函數是否被觸發?該表單是否有效? – tttony

+0

@tttony您所有問題的答案是「是」 – ReynierPM

+0

您是否啓用了電子郵件確認? – tttony

回答

1

經過挖掘和深入挖掘我的代碼後,我發現問題出在哪裏。由於@tttony用戶誰在燈泡打開,我,一不小心,刪除confirmation配置在config.yml因爲這個原因,該事件從未被觸發,那麼最簡單的辦法:

fos_user: 
    .... 
    registration: 
     confirmation: 
      from_email: 
       address:  [email protected] 
       sender_name: Myself 

現在才發現以前的解決方案之前,我配備了一個又一個,這讓我學習學習,我也離開這裏:

/** Disable the user by default - this is done in the event */ 
$user->setEnabled(false); 

/** @var $mailer FOS\UserBundle\Mailer\MailerInterface */ 
$mailer = $this->container->get('fos_user.mailer'); 

if (null === $user->getConfirmationToken()) { 
    $user->setConfirmationToken($str->generateRandomString(32)); 
} 

$mailer->sendConfirmationEmailMessage($user); 

$session = new Session(); 
$session->set('fos_user_send_confirmation_email/email', $user->getEmail()); 

我將無法在這裏做的唯一的事情就是用FOS\UserBundle\Util\TokenGeneratorInterface我不知道怎麼來的,所以如果知道如果將答案留給那個人,我將不勝感激,這兩種解決方案都能夠在我測試之後起作用。