繁体   English   中英

在FOSUserBundle中的REGISTRATION_SUCCESS事件之后重定向用户时出错

[英]Error in redirection of user after REGISTRATION_SUCCESS event in FOSUserBundle

我想在用户收到确认电子邮件之前将用户重定向到另一个表单。 所以我提交注册表后我想将用户重定向到另一个表单来填充,以便我可以为用户分配角色。 提交该表格后,用户应收到确认电子邮件。 确认后,用户将自动登录。

使用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

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

我收到错误:

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

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());
}
}

我知道这不是最佳实践解决方案,但它可以帮助您,只需更改选定的路线名称。

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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