简体   繁体   English

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

[英]Symfony 2 : FOSUserBundle : Using FOSUserEvents for Redirection

I have implemented FOSUserBundle in my Symfony 2 project to manage my members. 我已经在Symfony 2项目中实现了FOSUserBundle来管理我的成员。 Each time a form is successfully filled (registration, changing password...) I redirect the user to my homepage. 每次成功填写表单(注册,更改密码...)时,我都会将用户重定向到我的主页。 To do this I use the SUCCESS events provided by the bundle (see the documentation : Hooking into the controllers ). 为此,我使用了捆绑软件提供的SUCCESS事件(请参阅文档:连接到控制器 )。

It works fine for CHANGE_PASSWORD_SUCCESS and PROFILE_EDIT_SUCCESS events but it doesn't work for the FOSUserEvents::REGISTRATION_SUCCESS event. 它适用于CHANGE_PASSWORD_SUCCESS和PROFILE_EDIT_SUCCESS事件,但不适用于FOSU​​serEvents :: REGISTRATION_SUCCESS事件。 I am not redirected after user registration (the default bundle 'show profile' page is displayed). 用户注册后,我没有重定向(显示默认的捆绑包“显示配置文件”页面)。 EDIT : (the default bundle 'check email' page is displayed as the option registration confirmation by email is enabled). 编辑:(默认的捆绑包“检查电子邮件”页面显示为启用通过电子邮件的选项注册确认)。

I have implemented this code . 我已经实现了这段代码

Can someone help me to understand why ? 有人可以帮我理解为什么吗? My code here below: 我的代码如下:

Thank you for your help. 谢谢您的帮助。

The listener : 听众:

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

The service : 服务 :

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

I think you are hooking the wrong event. 我认为您错了。 According to what I've found in the RegistrationController of FosUserBundle , you have to subscribe to FOSUserEvents::REGISTRATION_COMPLETED event. 根据我在FosUserBundleRegistrationController中找到的内容 ,您必须订阅FOSUserEvents::REGISTRATION_COMPLETED事件。

I confirm the solution proposed by Qoop in the 5th comment of the main post. 我确认Qoop在主要帖子的第五条评论中提出的解决方案。

As the registration confirmation by email is enabled, the email confirmation listener is used. 由于启用了通过电子邮件的注册确认,因此将使用电子邮件确认侦听器。 This listener sets a response on FOSUserEvents::REGISTRATION_SUCCESS after my own listener. 此侦听器在我自己的侦听器之后在FOSUserEvents::REGISTRATION_SUCCESS上设置响应。 So I had to change the priority of my listener in order to set the response : 因此,我必须更改侦听器的优先级才能设置响应:

So here is the modified listener : 所以这是修改后的监听器:

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

Actually i think the right answer is in the doc 实际上,我认为正确的答案在文档中

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

Registration success listener with enabled confirmation at the same time¶ 注册成功监听器同时启用确认¶

When you have registration confirmation and you want to hook up to FOSUserEvents::REGISTRATION_SUCCESS event you will have to prioritize this listener to be called before FOS\\UserBundle\\EventListener\\EmailConfirmationListener::onRegistrationSuccess: 当您获得注册确认并且想要挂接到FOSUserEvents :: REGISTRATION_SUCCESS事件时,您必须在FOS \\ UserBundle \\ EventListener \\ EmailConfirmationListener :: onRegistrationSuccess之前确定要优先调用的侦听器:

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

If you don't do it, EmailConfirmationListener will be called earlier and you will be redirected to fos_user_registration_check_email route. 如果您不这样做,则将更早调用EmailConfirmationListener,并将您重定向到fos_user_registration_check_email路由。

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

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