fosuserbundle:使用事件列表器设置api密钥

时间:2015-07-04 15:27:40

标签: symfony fosuserbundle symfony-2.3

我使用的是symfony 2.3和FosUserBundle~2.0@dev。我想在成功注册后为每个用户设置一个api密钥。我为此目的实现了一个事件列表器,但它不起作用。我没有找到问题的确切位置 RegistrationConfirmListener

class RegistrationConfirmListener implements EventSubscriberInterface {

private $router;
private $em;

public function __construct(UrlGeneratorInterface $router, \Doctrine\ORM\EntityManager $em) {
    $this->em = $em;
    $this->router = $router;
}

/**
 * {@inheritDoc}
 */
public static function getSubscribedEvents() {
    return array(
        FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'
    );
}

public function onRegistrationSuccess(\FOS\UserBundle\Event\FormEvent $event) {
    $user = $event->getForm()->getData();
    // génération d'un unique key ,ici on a le id comme prefix donc garantie pas de duplication du key
    $user->setApiKey(md5(uniqid($user->getId(), TRUE)));
    $url = $this->router->generate('biginfo_admin_homepage');
    $event->setResponse(new RedirectResponse($url));
}

}  

service.yml

services:            
biginfo_user.registration_complet:
    class: Biginfo\UserBundle\EventListener\RegistrationConfirmListener
    arguments: [@router,@doctrine.orm.entity_manager]
    tags:
        - { name: kernel.event_subscriber}

1 个答案:

答案 0 :(得分:0)

您应该收听FOSUserEvents :: REGISTRATION_SUCCESS事件。因为REGISTRATION_COMPLETED事件侦听器方法接收到FOS \ UserBundle \ Event \ FilterUserResponseEvent实例,这不适合您的情况。