簡體   English   中英

FOSUserBundle:使用EventListener注冊后重定向用戶

[英]FOSUserBundle : Redirect the user after register with EventListener

我想在注冊之后將用戶重定向到另一個表單,然后才能訪問我網站上的任何內容(例如https://github.com/FriendsOfSymfony/FOSUserBundle/issues/387 )。

所以我在doc中創建了一個eventListener:

<?php
namespace rs\UserBundle\EventListener;

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

/**
 * Listener responsible to change the redirection at the end of the password resetting
 */
class RegistrationConfirmedListener implements EventSubscriberInterface
{
    private $router;

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

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

    public function onRegistrationConfirmed()
    {
        $url = $this->router->generate('rsWelcomeBundle_check_full_register');
        $response = new RedirectResponse($url);
        return $response;
    }
}

Services.yml:

services:
    rs_user.registration_completed:
        class: rs\UserBundle\EventListener\RegistrationConfirmedListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

但它不起作用,用戶注冊,他點擊他郵箱中的確認鏈接,他沒有被重定向到我想要的頁面上,他被記錄了,我只是有消息誰說帳戶已經確認。

為什么它不會將我重定向到路線:rsWelcomeBundle_check_full_register就像我想要的那樣?

謝謝

要完成您想要的任務,您應該使用FOSUserEvents::REGISTRATION_CONFIRM而不是FOSUserEvents::REGISTRATION_CONFIRMED

然后,您必須重寫您的類RegistrationConfirmedListener如:

class RegistrationConfirmListener implements EventSubscriberInterface
{
    private $router;

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

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

    public function onRegistrationConfirm(GetResponseUserEvent $event)
    {
        $url = $this->router->generate('rsWelcomeBundle_check_full_register');

        $event->setResponse(new RedirectResponse($url));
    }
}

還有你的service.yml

services:
    rs_user.registration_complet:
        class: rs\UserBundle\EventListener\RegistrationConfirmListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

REGISTRATION_CONFIRM收到一個FOS\\UserBundle\\Event\\GetResponseUserEvent實例,如下所示: https//github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php

它允許您修改將要發送的響應: https//github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Event/GetResponseUserEvent.php

"friendsofsymfony/user-bundle": "2.0.x-dev",

不確定為什么接受的答案適合您,因為REGISTRATION_CONFIRM在確認令牌后發生。

如果你想執行一個動作,在FOS registerAction之后用一些額外的形式重定向到另一個頁面,我建議采用以下方法。

這是FOS提交的表單生效后在registerAction上執行的代碼:

FOS \\ UserBundle \\控制器\\這個RegistrationController

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

正如你所看到的那樣,第一個可能的返回發生在FOSUserEvents :: REGISTRATION_SUCCESS事件之后,如果響應為null ,在我的情況下沒有,因為我已經配置了郵件程序來發送確認令牌而FOS正在使用偵聽此FOSUserEvents的偵聽器:: REGISTRATION_SUCCESS事件,並在發送電子郵件后設置重定向響應。

FOS \\ UserBundle \\事件監聽\\ EmailConfirmationListener

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

/**
 * @param FormEvent $event
 */
public function onRegistrationSuccess(FormEvent $event)
{
    /** @var $user \FOS\UserBundle\Model\UserInterface */
    $user = $event->getForm()->getData();

    $user->setEnabled(false);
    if (null === $user->getConfirmationToken()) {
        $user->setConfirmationToken($this->tokenGenerator->generateToken());
    }

    $this->mailer->sendConfirmationEmailMessage($user);

    $this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());

    $url = $this->router->generate('fos_user_registration_check_email');
    $event->setResponse(new RedirectResponse($url));
}

好,我懂了! 那么如何重定向到另一個頁面?

我建議覆蓋checkEmailAction,因為很可能你不想覆蓋發送電子郵件的監聽器,因為這是你工作流程的一部分。

只是:

TB \\ UserBundle \\控制器\\這個RegistrationController

/**
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function checkEmailAction()
{
    /** @var UserManager $userManager */
    $userManager = $this->get('fos_user.user_manager');
    /** @var string $email */
    $email = $this->get('session')->get('fos_user_send_confirmation_email/email');

    $user = $userManager->findUserByEmail($email);

    return $this->redirect($this->generateUrl('wall', ['username' => $user->getUsername()]));
}

正如您所看到的,我決定將用戶重定向到他的新配置文件而不是呈現FOS的check_email模板。

Docs如何覆蓋控制器: https//symfony.com/doc/master/bundles/FOSUserBundle/overriding_controllers.html (基本上為您的捆綁包定義一個父級,並在目錄中創建一個與FOS同名的文件。)

如果您沒有使用確認電子郵件,則可以在以這種方式提交注冊表后立即重定向用戶:

class RegistrationConfirmationSubscriber implements EventSubscriberInterface
{
    /** @var Router */
    private $router;

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

    public static function getSubscribedEvents()
    {
        return [FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationConfirm'];
    }

    public function onRegistrationConfirm(FilterUserResponseEvent $event)
    {
        /** @var RedirectResponse $response */
        $response = $event->getResponse();
        $response->setTargetUrl($this->router->generate('home_route'));
    }
}

訂閱者聲明保持不變:

registration_confirmation_subscriber:
    class: AppBundle\Subscriber\RegistrationConfirmationSubscriber
    arguments:
        - "@router"
    tags:
        - { name: kernel.event_subscriber }

路由重定向也可以使用:

fos_user_registration_confirmed:
    path: /register/confirmed
    defaults:
        _controller: FrameworkBundle:Redirect:redirect
        route: redirection_route
        permanent: true

要獲得快速解決方案:您還可以覆蓋路線。 假設你想重定向到你的主頁,你可以這樣做:

 /**
 * @Route("/", name="index")
 * @Route("/", name="fos_user_registration_confirmed")
 * @Template(":Default:index.html.twig")
 */
public function indexAction()
{

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM