活动FOSUserBundle Symfony2注册和登录 [英] Events FOSUserBundle Symfony2 on register and login
问题描述
我试图在FOSUserBundle上实现事件
I'm trying to implement events on FOSUserBundle
<?php
namespace EasyApp\UserBundle\Service;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine;
use EasyApp\UserBundle\Entity\UserLogin;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserBundle;
class LoginManager implements EventSubscriberInterface
{
/** @var \Symfony\Component\Security\Core\SecurityContext */
private $securityContext;
/** @var \Doctrine\ORM\EntityManager */
private $em;
/**
* Constructor
*
* @param SecurityContext $securityContext
* @param Doctrine $doctrine
*/
public function __construct(SecurityContext $securityContext, Doctrine $doctrine)
{
$this->securityContext = $securityContext;
$this->em = $doctrine->getEntityManager();
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onSecurityImplicitLogin',
FOSUserEvents::REGISTRATION_COMPLETED=> 'onRegistrationCompleted'
);
}
public function onSecurityImplicitLogin(UserEvent $event)
{
die;
$user = $event->getAuthenticationToken()->getUser();
$request = $event->getRequest();
if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
// user has just logged in
$this->saveLogin($request, $user);
}
}
public function onRegistrationCompleted(FilterUserResponseEvent $event){
$user = $event->getAuthenticationToken()->getUser();
$request = $event->getRequest();
saveLogin($request, $user);
}
public function saveLogin($request, $user){
$login = new UserLogin();
$login->setIp($request->getClientIp());
$login->setUser($user);
$this->em->persist($login);
$this->em->flush();
}
}
我的服务
services:
user_login_manager:
class: 'EasyApp\UserBundle\Service\LoginManager'
arguments: ['@security.context', '@doctrine']
tags:
- { name: 'kernel.event_subscriber', event: 'fos_user.security.interactive_login'}
- { name: 'kernel.event_subscriber', event: 'fos_user.registration.completed'}
但是我有问题。当我没有登录时,$ code> getSubscribedEvents()被调用,但不是 onSecurityImplicitLogin(UserEvent $ event)
But I have problems. When I login nothing happen, the getSubscribedEvents()
is called but not onSecurityImplicitLogin(UserEvent $event)
另一个问题是注册。以下错误发生在 onSecurityImplicitLogin(UserEvent $ event)
And the other problem is on register. Following error occurs on onSecurityImplicitLogin(UserEvent $event)
Catchable Fatal Error: Argument 1 passed to EasyApp\UserBundle\Service\LoginManager::onSecurityImplicitLogin() must be an instance of EasyApp\UserBundle\Service\UserEvent, instance of FOS\UserBundle\Event\UserEvent given in /Users/antoine/Documents/projects/easyApp/application/src/EasyApp/UserBundle/Service/LoginManager.php line 46
,如果我发表了这一行,我在 onRegistrationCompleted(FilterUserResponseEvent $ event)上有相同的错误
and if I comment this line I got the same error on onRegistrationCompleted(FilterUserResponseEvent $event)
编辑
services:
user_login_manager:
class: 'EasyApp\UserBundle\Service\LoginManager'
arguments: ['@security.context', '@doctrine']
tags:
- { name: 'kernel.event_subscriber'}
- { name: 'kernel.event_listener', event: 'security.interactive_login' }
推荐答案
两点:
-
在服务定义
- {name:'kernel.event_subscriber'}
就够了,没有事件输入。这与用户和听众之间的区别。监听器只能听服务定义中的
事件
和方法
params定义的一个事件。订阅者可以通过方法getSubscribedEvents()
定义多个事件,因此不需要服务定义中的其他参数。
In the service definition
- { name: 'kernel.event_subscriber'}
is enough, no event entry. This the difference between subscriber and listener. A listener can only listen to one event, defined troughevent
andmethod
params in the service definition. A subscriber can listen to multiple events, defined trough the methodgetSubscribedEvents()
, so no further params in the service definition needed.
第二点,你是类型暗示的事件类( UserEvent
和 FilterUserResponseEvent
),但是您从未导入它们,因此PHP会将它们置于相同的命名空间中(如错误消息中所述)。
Second point, you are type hinting against the event classes (UserEvent
andFilterUserResponseEvent
), but you never imported them, so PHP assumes them in the same namespace (as said in the error message).
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\Event\FilterUserResponseEvent;
这篇关于活动FOSUserBundle Symfony2注册和登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!