Symfony 2 : FOSUserBundle : Using FOSUserEvents for Redirect


Symfony 2 : FOSUserBundle : Using FOSUserEvents for Redirection

我已经在我的Symfony 2项目中实现了FOSUserBundle来管理我的成员。每次成功填写表格时(注册,更改密码...我将用户重定向到我的主页。为此,我使用捆绑包提供的 SUCCESS 事件(请参阅文档:挂钩到控制器)。

它适用于CHANGE_PASSWORD_SUCCESS和PROFILE_EDIT_SUCCESS事件,但不适用于 FOSUserEvents::REGISTRATION_SUCCESS 事件。用户注册后,我没有被重定向(显示默认捆绑包"显示个人资料"页面)。编辑 :(默认捆绑包"检查电子邮件"页面显示为启用通过电子邮件注册确认的选项)。

我已经实现了这段代码。

有人可以帮助我理解为什么吗?我的代码在下面:

谢谢你的帮助。

听众 :

<?php
// src/FBN/UserBundle/EventListener/FormSuccessListener.php
namespace FBN'UserBundle'EventListener;
use FOS'UserBundle'FOSUserEvents;
use FOS'UserBundle'Event'FormEvent;
use Symfony'Component'EventDispatcher'EventSubscriberInterface;
use Symfony'Component'HttpFoundation'RedirectResponse;
use Symfony'Component'Routing'Generator'UrlGeneratorInterface;
/**
 * Listener responsible to change the redirection when a form is successfully filled
 */
class FormSuccessListener implements EventSubscriberInterface
{
    private $router;
    public function __construct(UrlGeneratorInterface $router)
    {
        $this->router = $router;
    }
    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_SUCCESS => 'onFormSuccess',    // Not working
            FOSUserEvents::CHANGE_PASSWORD_SUCCESS => 'onFormSuccess', // Working
            FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onFormSuccess',    // Working                   
        );
    }
    public function onFormSuccess(FormEvent $event)
    {
        $url = $this->router->generate('fbn_guide_homepage');
        $event->setResponse(new RedirectResponse($url));
    }
}

服务范围 :

services:
    fbn_user.formsuccess_listener:
        class: FBN'UserBundle'EventListener'FormSuccessListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

我认为你钩错了事件。根据我在FosUserBundle的注册控制器中找到的内容,您必须订阅FOSUserEvents::REGISTRATION_COMPLETED事件。

我确认 Qoop 在主帖子的第 5 条评论中提出的解决方案。

启用通过电子邮件进行注册确认时,将使用电子邮件确认侦听器。此侦听器在我自己的侦听器之后设置FOSUserEvents::REGISTRATION_SUCCESS响应。所以我不得不更改听众的优先级才能设置响应:

所以这是修改后的侦听器:

// src/FBN/UserBundle/EventListener/FormSuccessListener.php
namespace FBN'UserBundle'EventListener;
use FOS'UserBundle'FOSUserEvents;
use FOS'UserBundle'Event'FormEvent;
use Symfony'Component'EventDispatcher'EventSubscriberInterface;
use Symfony'Component'HttpFoundation'RedirectResponse;
use Symfony'Component'Routing'Generator'UrlGeneratorInterface;
/**
 * Listener responsible to change the redirection when a form is successfully filled
 */
class FormSuccessListener implements EventSubscriberInterface
{
    private $router;
    public function __construct(UrlGeneratorInterface $router)
    {
        $this->router = $router;
    }
    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_SUCCESS => array('onFormSuccess',-10),
            FOSUserEvents::CHANGE_PASSWORD_SUCCESS => array('onFormSuccess',-10), 
            FOSUserEvents::PROFILE_EDIT_SUCCESS => array('onFormSuccess',-10),                        
        );
    }
    public function onFormSuccess(FormEvent $event)
    {
        $url = $this->router->generate('fbn_guide_homepage');
        $event->setResponse(new RedirectResponse($url));
    }
}

实际上我认为正确的答案在文档中

http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html

同时启用确认的注册成功侦听器¶

当您有注册确认并且想要挂接到 FOSUserEvents::REGISTRATION_SUCCESS 事件时,您必须在 FOS''UserBundle''EventListener''EmailConfirmationListener::onRegistrationSuccess 之前优先调用此侦听器:

public static function getSubscribedEvents()
{
    return [
        FOSUserEvents::REGISTRATION_SUCCESS => [
            ['onRegistrationSuccess', -10],
        ],
    ];
}

如果您不这样做,电子邮件确认侦听器将被更早调用,您将被重定向到fos_user_registration_check_email路由。