Symfony2:注册后发送支票电子邮件

问题描述 投票:2回答:3

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

fos_user:
    registration:
        confirmation:
            enabled:    true

效果很好:电子邮件发送成功。我被重定向到/ check-email页面,该页面已发送此电子邮件。但是,我想更改重定向:我想重定向到我的索引页而不是/ 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 }

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

无论哪种情况,希望您能对我有所帮助!

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

symfony fosuserbundle
3个回答
0
投票

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


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 }

0
投票

[我知道这个问题已经发布很久了,但是我找到了一个解决方案,我想与有同样问题的其他人分享。

在这种情况下,您应该使用的事件是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 }

我希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.