Symfony 2:FOSUserBundle:使用FOSUserEvents进行重定向

时间:2015-02-04 08:30:31

标签: php symfony fosuserbundle

我在Symfony 2项目中实施了FOSUserBundle来管理我的成员。每次成功填写表格(注册,更改密码......)我都会将用户重定向到我的主页。为此,我使用捆绑包提供的SUCCESS事件(请参阅文档:Hooking into the controllers)。

它适用于CHANGE_PASSWORD_SUCCESS和PROFILE_EDIT_SUCCESS事件,但它不适用于FOSU​​serEvents :: REGISTRATION_SUCCESS事件。用户注册后我没有被重定向(显示默认包“显示配置文件”页面)。 编辑(显示默认包'检查电子邮件'页面,因为已启用电子邮件选项注册确认)。

我已实施此code

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

感谢您的帮助。

听众:

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

3 个答案:

答案 0 :(得分:0)

我认为你正在勾结错误的事件。 根据我在RegistrationController of FosUserBundle中找到的内容,您必须订阅FOSUserEvents::REGISTRATION_COMPLETED个活动。

答案 1 :(得分:0)

我确认了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));
    }
}

答案 2 :(得分:0)

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

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

如果您不这样做,将提前调用EmailConfirmationListener,您将被重定向到fos_user_registration_check_email路由。