2014-03-12 165 views
1

所以,我試圖改變註冊後與FOS重定向網址。FosUser,註冊後重定向

所以,我必須創建一個自定義RegistrationConfirmListener

<?php 
namespace project\ApplicationBundle\EventListener; 

use FOS\UserBundle\FOSUserEvents; 
use FOS\UserBundle\Event\UserEvent; 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 

/** 
* Listener responsible to change the redirection at the end of the password resetting 
*/ 
class RegistrationConfirmListener implements EventSubscriberInterface 
{ 
    private $router; 

    public function __construct(UrlGeneratorInterface $router) 
    { 
     $this->router = $router; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    public static function getSubscribedEvents() 
    { 
     return array(
       FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm' 
     ); 
    } 

    public function onRegistrationConfirm(GetResponseUserEvent $event) 
    { 
     $url = $this->router->generate('myUrlForRedirect'); 

     $event->setResponse(new RedirectResponse($url)); 
    } 
} 

並添加幾行代碼寫進我的services.yml

rs_user.registration_complet: 
     class: ThanksWho\ApplicationBundle\EventListener\RegistrationConfirmListener 
     arguments: [@router] 
     tags: 
      - { name: kernel.event_subscriber } 

但是當我測試,我總是重定向到/confirmed,而不是myUrlForRedirect

任何想法?

+0

神似http://stackoverflow.com/questions/16427267/fosuserbundle-redirect-the-user-after- register-with-eventlistener,更多信息在那裏 –

回答

0

我想你省略了在你的監聽器中導入GetResponseUserEvent類。您應該更換use FOS\UserBundle\Event\UserEvent;use FOS\UserBundle\Event\GetResponseUserEvent;

0

我發現我的問題。

我忘了補充:

public function getParent() 
    { 
     return 'FOSUserBundle'; 
    } 
在我BundleClass

...