Symfony2-是否有一种方法可以挂接到事件中进行用户确认


Symfony2 - Is there a way to hook into an event for user confirmation?

我正在为symfony2使用FOS用户捆绑包,并希望在用户在/register/confirm/{token} 确认注册时运行一些自定义代码来记录事件

然而,当用户被确认时似乎没有事件,所以我想知道当用户帐户被确认时挂接执行的最佳方式。

您可以覆盖RegistrationController(请参阅文档)以添加日志记录功能。

如果您能够使用dev master(2.0.*@dev),则可以使用FOSUserBundle中的新控制器事件。有关详细信息,请参阅github上的文档:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/controller_events.md

以下是confirm事件的一个小示例。不要忘记定义上面链接中提到的服务。

<?php
namespace Acme'UserBundle'Security'Listener;
use FOS'UserBundle'Event'GetResponseUserEvent;
use FOS'UserBundle'FOSUserEvents;
use Symfony'Component'EventDispatcher'EventSubscriberInterface;
class RegistrationListener implements EventSubscriberInterface
{
    public function __construct(/** inject services you need **/)
    {
        // assign services to private fields
    }
    public static function getSubscribedEvents()
    {
        return array(FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm',);
    }
    /**
     * GetResponseUserEvent gives you access to the user object
     **/
    public function onRegistrationConfirm(GetResponseUserEvent $event)
    {
        // do your stuff
    }
}