我正在构建“上次登录”功能。在创建activitylistener并将侦听器定义为服务之后,我收到错误
ContextErrorException:Catchable Fatal Error:传递给Acme \ SecurityBundle \ EventListener \ ActivityListener :: __ construct()的参数2必须是Doctrine \ ORM \ EntityManager的实例,Doctrine \ Bundle \ DoctrineBundle \ Registry的实例,在C中调用:第350行的\ xampp \ htdocs \ sinfoc \ public \ app \ cache \ dev \ appDevDebugProjectContainer.php,在C:\ xampp \ htdocs \ sinfoc \ public \ src \ Acme \ SecurityBundle \ EventListener \ ActivityListener.php第22行中定义< / p>
这是我的ActivityListener类
public function __construct(SecurityContext $securityContext, EntityManager $entityManager)
{
$this->securityContext = $securityContext;
$this->entityManager = $entityManager;
}
这里是services.yml,其中侦听器被定义为服务
services:
activity_listener:
class: Bnpp\SecurityBundle\EventListener\ActivityListener
arguments: [@security.context, @doctrine.orm.entity_manager]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onCoreController }
这是完整的ActivityListenerClass
<?php
namespace Bnpp\SecurityBundle\EventListener;
use Doctrine\ORM\EntityManager;
//use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Bnpp\SecurityBundle\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Registry;
/**
* Listener that updates the last activity of the authenticated user
*/
class ActivityListener
{
protected $securityContext;
protected $entityManager;
public function __construct(SecurityContext $securityContext, EntityManager $entityManager)
{
$this->securityContext = $securityContext;
$this->entityManager = $entityManager;
}
/**
* Update the user "lastActivity" on each request
* @param FilterControllerEvent $event
*/
public function onCoreController(FilterControllerEvent $event)
{
// Check that the current request is a "MASTER_REQUEST"
// Ignore any sub-request
if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
return;
}
// Check token authentication availability
if ($this->securityContext->getToken()) {
$user = $this->securityContext->getToken()->getUser();
if ( ($user instanceof User) && !($user->isActiveNow()) ) {
$user->setLastActivity(new \DateTime('now'));
$this->entityManager->flush($user);
}
}
}
}
答案 0 :(得分:0)
从此错误中
ContextErrorException:Catchable Fatal Error:传递给Acme \ SecurityBundle \ EventListener \ ActivityListener :: __ construct()的参数2必须是Doctrine \ ORM \ EntityManager的实例,Doctrine \ Bundle \ DoctrineBundle \ Registry的实例,在C中调用:第350行的\ xampp \ htdocs \ sinfoc \ public \ app \ cache \ dev \ appDevDebugProjectContainer.php,在C:\ xampp \ htdocs \ sinfoc \ public \ src \ Acme \ SecurityBundle \ EventListener \ ActivityListener.php第22行中定义< / p>
你要提供
学说\ ORM \ EntityManager的,
但你提供了
学说\捆绑\ DoctrineBundle \注册表
at
arguments: [@security.context, @doctrine.orm.entity_manager]
你的论点应该是
arguments:
- "@security.context"
- "@doctrine.orm.entity_manager"