【问题标题】:Symfony 2 : FOSUserBundle : Using FOSUserEvents for RedirectionSymfony 2:FOSUserBundle:使用 FOSUserEvents 进行重定向
【发布时间】:2015-02-04 08:30:30
【问题描述】:

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

它适用于 CHANGE_PASSWORD_SUCCESS 和 PROFILE_EDIT_SUCCESS 事件,但不适用于 FOSUserEvents::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 }

【问题讨论】:

  • 您使用的是电子邮件确认吗?
  • @Qoop 是的,我愿意。确切地说,我想避免重定向到注册确认页面,并在我的主页上显示一条消息。
  • 那么在您通过电子邮件确认之后,它会将您带到注册确认页面?在这种情况下,您应该收听的事件是 FOSUserEvents::REGISTRATION_CONFIRM,但这确实需要 GetResponseUserEvent 而不是 FormEvent,因此您应该相应地处理它。 (github.com/FriendsOfSymfony/FOSUserBundle/blob/master/…)
  • 不完全是。当您使用经过验证的表单进行注册时,您将被重定向到显示已发送电子邮件的页面。此时,我想将用户重定向到我的主页并显示一条消息(一封电子邮件已发送......)。稍后当用户通过单击电子邮件的链接进行确认时,将使用 RegistrationController 的方法 confirmAction()。所以我认为正确的事件是 REGISTRATION_SUCCESS。
  • 对了。可能是在您的侦听器之后调用电子邮件确认侦听器,这意味着由于稍后将它们添加到调度程序的顺序,正在再次设置响应。更改优先级,以便您的优先级在 FOS 侦听器可能对其进行排序之后,所以 FOSUserEvents::REGISTRATION_SUCCESS =&gt; array('onFormSuccess', -10),

标签: php symfony fosuserbundle


【解决方案1】:

我认为你挂错了事件。 根据我在RegistrationController of FosUserBundle 中找到的信息,您必须订阅FOSUserEvents::REGISTRATION_COMPLETED 事件。

【讨论】:

  • 谢谢,但我认为我使用了正确的事件,因为它适用于其他两个事件。所有控制器都具有相同的结构。所以看看第 57 到 66 行:github.com/FriendsOfSymfony/FOSUserBundle/blob/master/…。此外,文档说:'因此,如果您不想要默认重定向,所有 SUCCESS 事件都允许您设置响应。并且所有 COMPLETED 事件都允许您在响应返回之前访问它。'
  • COMPLETED 事件使用 FOS\UserBundle\Event\FilterUserResponseEvent 作为侦听器方法。此类不包含 setResponse() 方法。 SUCCESS 事件使用包含 setResponse() 方法的 FOS\UserBundle\Event\FormEvent。
  • 如您所见here,控制器总是在您连接到事件后重定向到fos_user_registration_confirmed。所以...
  • 是的,但首先,此代码测试事件的 $response 属性是否为空(参见第 63 行)。侦听器允许捕获事件(FormEvent 实例参见第 58 行)并在控制器之前设置重定向(第 64/65 行)。所以如果事件的$response属性不为null,则不设置默认重定向。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    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 路由。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      • 2018-02-26
      • 1970-01-01
      相关资源
      最近更新 更多