Php Symfony在用户登录时执行操作
当用户成功登录时,我不想对数据库执行更新操作(将+1添加到登录计数和更新上次登录时间戳)。使用Symfony 3 我添加了列表器:Php Symfony在用户登录时执行操作,php,listener,symfony,Php,Listener,Symfony,当用户成功登录时,我不想对数据库执行更新操作(将+1添加到登录计数和更新上次登录时间戳)。使用Symfony 3 我添加了列表器: // src/AppBundle/Listener/SecurityListener.php namespace AppBundle\Listener; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\Security\Core\Authe
// src/AppBundle/Listener/SecurityListener.php
namespace AppBundle\Listener;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
#use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine;
use Doctrine\ORM\EntityManager;
class SecurityListener
{
private $tokenStorage;
public function __construct(TokenStorage $tokenStorage, EntityManager $doctrine)
{
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
if ( $this->tokenStorage->isGranted('IS_AUTHENTICATED_FULLY') ) {
if( $user = $event->getAuthenticationToken()->getUser() )
var_dump($user);
else {
var_dump('nej');
}
}
/**
* Update lastLogin and loginCount for user in database
*/
// $em->
}
}
app/config/services.yml:
# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/service_container.html
parameters:
# parameter_name: value
account.security_listener.class: AppBundle\Listener\SecurityListener
services:
# service_name:
# class: AppBundle\Directory\ClassName
# arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
account.security_listener:
class: %account.security_listener.class%
arguments: ['@security.token_storage', '@doctrine.orm.entity_manager']
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }
我得到了错误error:在null上调用成员函数isgrated()
方法isgrated('IS\u AUTHENTICATED\u FULLY')
在Symfony\Component\Security\Core\Authorization\AuthorizationChecker上可用。如果要检查用户是否通过令牌进行了身份验证(如在SecurityListener
类中),请使用以下方法更改onSecurityInteractiveLogin
方法:
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
if ($event->getAuthenticationToken()->isAuthenticated()) {
$user = $event->getAuthenticationToken()->getUser();
var_dump($user);
}
/**
* Update lastLogin and loginCount for user in database
*/
// $em->
}
你忘了:
public function __construct(TokenStorage $tokenStorage, EntityManager $doctrine)
{
$this->tokenStorage = $tokenStorage;
}
??此InteractiveEngineent的服务是什么?我收到错误:
服务“account.security\u listener”依赖于不存在的服务“security.interactiveEngineent”。
我也尝试了security.event,您不需要修改您的服务.yml。您只需要更新AppBundle\Listener\SecurityListener
类的onSecurityInteractiveLogin
方法。我得到错误:Catchable致命错误:传递给AppBundle\Listener\SecurityListener的参数3::\uu construct()必须是Symfony\Component\Security\Http\Event\interactiveEngineent的实例,复制修改后的代码时,未提供任何代码。