簡體   English   中英

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

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

我已經在Symfony 2項目中實現了FOSUserBundle來管理我的成員。 每次成功填寫表單(注冊,更改密碼...)時,我都會將用戶重定向到我的主頁。 為此,我使用了捆綁軟件提供的SUCCESS事件(請參閱文檔:連接到控制器 )。

它適用於CHANGE_PASSWORD_SUCCESS和PROFILE_EDIT_SUCCESS事件,但不適用於FOSU​​serEvents :: 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 }

我認為您錯了。 根據我在FosUserBundleRegistrationController中找到的內容 ,您必須訂閱FOSUserEvents::REGISTRATION_COMPLETED事件。

我確認Qoop在主要帖子的第五條評論中提出的解決方案。

由於啟用了通過電子郵件的注冊確認,因此將使用電子郵件確認偵聽器。 此偵聽器在我自己的偵聽器之后在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],
        ],
    ];
}

如果您不這樣做,則將更早調用EmailConfirmationListener,並將您重定向到fos_user_registration_check_email路由。

暫無
暫無

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

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