2

我第一次在这个论坛上发布消息,我经常使用。我在 Symfony2 应用程序中使用 FOSUserbundle 来管理用户。当用户通过以下方式创建帐户时,我激活了电子邮件确认的发送:

fos_user:
    registration:
        confirmation:
            enabled:    true

它工作得很好:电子邮件发送成功。我被重定向到 /check-email 页面,显示此电子邮件已发送。但是,我想更改重定向:我想重定向到我的索引页而不是 /check-email。所以我做了我的研究,我知道他必须经历 FOSUserBundle 事件(在此处列出)。

我做了什么 :

class RegistrationListener implements EventSubscriberInterface {

private $router;

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

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

public function onRegistrationConfirm(FormEvent $event) {
    $url = $this->router->generate('listeArticlesAccueil');
    $event->setResponse(new RedirectResponse($url));
}

}

和 services.yml

services: 
    listener_user.registration_listener:
        class: My\Soft\UserBundle\EventListener\RegistrationListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

问题是每次我被重定向到页面/check-email。因此,我告诉我这可能是错误的事件。所以我也尝试了 REGISTRATION_SUCCESS。但没有任何改变。所以要么我没有使用正确的事件,要么我做错了什么。

无论哪种情况,我希望你能帮助我!

非常感谢,对不起我的英语不好;)

4

3 回答 3

1

我知道这个问题很久以前就已经发布了,但我找到了一个解决方案,我想把它分享给有同样问题的其他人。

在这种情况下,您应该使用的事件是 REGISTRATION_SUCCESS。

您必须在 FOS\UserBundle\EventListener\EmailConfirmationListener::onRegistrationSuccess 之前优先调用侦听器 REGISTRATION_SUCCESS,如下所示:

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

如果您不这样做,将提前调用 EmailConfirmationListener,您将被重定向到 fos_user_registration_check_email 路由。

这是我所做的:

 class RedirectAfterRegistrationSubscriber implements EventSubscriberInterface
 {
private $router;

public function __construct(RouterInterface $router)
{
    $this->router = $router;
}
public function onRegistrationSuccess(FormEvent $event)
{
    $event->stopPropagation();
    $url = $this->router->generate('homepage');
    $response = new RedirectResponse($url);
    $event->setResponse($response);
}
public static function getSubscribedEvents()
{
    return [
        FOSUserEvents::REGISTRATION_SUCCESS => ['onRegistrationSuccess',-10],
    ];
}
 }

在 app/services.yml 中:

app.redirect_after_registration_subscriber:
    class: AppBundle\EventListener\RedirectAfterRegistrationSubscriber
    arguments: ['@router']
    tags:
        - { name: kernel.event_subscriber }

我希望这有帮助。

于 2020-06-11T09:42:37.880 回答
0

我会打赌 REGISTRATION_SUCCESS。文档(https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php)说这个事件允许设置响应。REGISTRATION_CONFIRM 只允许访问用户

于 2013-07-29T15:10:09.037 回答
0

而不是那个用途REGISTRATION_CONFIRM

下面是事件监听器的完整代码:

/**
 * Description of RegisterationConfirmListener
 *
 * @author imran
 */
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Description of RegisterationConfirmListener
 *
 * @author imran
 */
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
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 RegisterationConfirmListener implements EventSubscriberInterface {
    private $router;

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

    public static function getSubscribedEvents() {
        return [
            FOSUserEvents::REGISTRATION_CONFIRM => 'onRegisterationConfirm',
        ];
    }

    public function onRegisterationConfirm(GetResponseUserEvent $event) {
        $url = $this->router->generate('home');
        $event->setResponse(new RedirectResponse($url));
    }
}

对于Services.yml

app_user.registeration_confirmed:
    class: Wishlocker\AppBundle\EventListener\RegisterationConfirmListener
    arguments: [ @router ]
    tags:
        - { name: kernel.event_subscriber }
于 2014-10-21T23:14:17.637 回答