2016-05-25 10 views
1

確認メールを受け取る前に、別のフォームにリダイレクトしようとしています。だから私は登録フォームを提出した後、私はユーザにロールを割り当てることができるように、別のフォームにユーザをリダイレクトしたいと思います。そのフォームを提出したら、確認メールを受け取ってください。確認後、ユーザーは自動的にログインします。私はのgettinていFOSUserBundleのREGISTRATION_SUCCESSイベント後のユーザーのリダイレクトエラー

services: 
usr_user.registration.success: 
    class: Usr\UserBundle\EventListener\RegistrationSuccessListener 
    arguments: ["@router"] 
    tags: 
     - { name: kernel.event_subscriber } 

完全なコード User management with FUB

RegistrationSuccessListener.php

namespace Usr\UserBundle\EventListener; 

use FOS\UserBundle\FOSUserEvents; 
use FOS\UserBundle\Event\GetResponseUserEvent; 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 

class RegistrationSuccessListener implements EventSubscriberInterface 
{ 
private $router; 

public function __construct(UrlGeneratorInterface $router) 
{ 
    $this->router = $router; 
} 

/** 
* {@inheritDoc} 
*/ 
public static function getSubscribedEvents() 
{ 
    return array(
     FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess' 
    ); 
} 

public function onRegistrationSuccess(GetResponseUserEvent $event) 
{ 
    $url = $this->router->generate('fos_user_registration_success'); 
    $event->setResponse(new RedirectResponse($url)); 
} 
} 

そして、私のservices.ymlグラム・エラー:

Type error: Argument 1 passed to Usr\UserBundle\EventListener\RegistrationSuccessListener::onRegistrationSuccess() must be an instance of FOS\UserBundle\Event\GetResponseUserEvent, instance of FOS\UserBundle\Event\FormEvent given 

RegistrationController.phpだけREGISTRATION_SUCCESSを呼び出した後で立ち往生コードとして、問題を解決していません

namespace Usr\UserBundle\Controller; 

use FOS\UserBundle\FOSUserEvents; 
use FOS\UserBundle\Event\FormEvent; 
use FOS\UserBundle\Event\GetResponseUserEvent; 
use FOS\UserBundle\Event\FilterUserResponseEvent; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 
use FOS\UserBundle\Model\UserInterface; 
use FOS\UserBundle\Controller\RegistrationController as BaseController; 

/** 
* Controller managing the registration 
*/ 
class RegistrationController extends BaseController 
{ 
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(), 
    )); 
} 

/** 
* Tell the user to check his email provider 
*/ 
public function checkEmailAction() 
{ 
    $email = $this->get('session')->get('fos_user_send_confirmation_email/email'); 
    $this->get('session')->remove('fos_user_send_confirmation_email/email'); 
    $user = $this->get('fos_user.user_manager')->findUserByEmail($email); 

    if (null === $user) { 
     throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email)); 
    } 

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

/** 
* Receive the confirmation token from user email provider, login the user 
*/ 
public function confirmAction(Request $request, $token) 
{ 
    /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */ 
    $userManager = $this->get('fos_user.user_manager'); 

    $user = $userManager->findUserByConfirmationToken($token); 

    if (null === $user) { 
     throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token)); 
    } 

    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */ 
    $dispatcher = $this->get('event_dispatcher'); 

    $user->setConfirmationToken(null); 
    $user->setEnabled(true); 

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

    $userManager->updateUser($user); 

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

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

    return $response; 
} 

/** 
* Tell the user his account is now confirmed 
*/ 
public function confirmedAction() 
{ 
    $user = $this->getUser(); 
    if (!is_object($user) || !$user instanceof UserInterface) { 
     throw new AccessDeniedException('This user does not have access to this section.'); 
    } 

    return $this->render('FOSUserBundle:Registration:confirmed.html.twig', array(
     'user' => $user, 
     'targetUrl' => $this->getTargetUrlFromSession(), 
    )); 
} 

private function getTargetUrlFromSession() 
{ 
    // Set the SecurityContext for Symfony <2.6 
    if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) { 
     $tokenStorage = $this->get('security.token_storage'); 
    } else { 
     $tokenStorage = $this->get('security.context'); 
    } 

    $key = sprintf('_security.%s.target_path', $tokenStorage->getToken()->getProviderKey()); 

    if ($this->get('session')->has($key)) { 
     return $this->get('session')->get($key); 
    } 
} 

public function registercuccessAction() 
{ 
    return $this->render('::base.html.twig', array()); 
} 
} 

答えて

0

I know that is not the best practice solution but it can help you, just change the selected route name.

enter image description here

+0

ITSは、その敗走変更は決してありません実行される。 – napestershine

関連する問題