Symfony2:注册后检查电子邮件

时间:2013-07-29 13:46:41

标签: symfony fosuserbundle

我第一次在这个论坛发帖子,我经常使用。我在Symfony2应用程序中使用FOSUserbundle来管理用户。当用户通过以下内容创建帐户时,我已激活发送电子邮件确认:

fos_user:
    registration:
        confirmation:
            enabled:    true

效果很好:电子邮件发送成功。我被重定向到页面/检查电子邮件,上面写着此电子邮件已发送。但是,我想更改重定向:我想重定向到我的索引页面而不是/ check-email。所以我做了我的研究,我知道他必须经历FOSUserBundle事件(list here)。

我做了什么:

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 }

问题在于每次重定向到页面/检查电子邮件时。因此,我告诉我,这可能是错误的事件。所以我也尝试了REGISTRATION_SUCCESS。但没有变化。所以要么我没有使用正确的事件,要么我做错了。

在任何一种情况下,我希望你能帮助我!

非常感谢,抱歉我的英语不好;)

3 个答案:

答案 0 :(得分: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 }

我希望这会有所帮助。

答案 1 :(得分:0)

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

答案 2 :(得分: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 }