2014-03-12 125 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

...