Symfony 4:事件侦听器无法自动装配用户界面
Symfony 4: Event listener Cannot autowire UserInterface
当我将 UserInterface 添加到我的事件侦听器时,出现以下错误:
Cannot autowire service "App\EventListener\ControllerListener": argument "$user" of method "__construct()" references interface "Symfony\Component\Security\Core\User\UserInterface" but no such service exists. It cannot be auto-registered because it is from a different root namespace.
我不知道发生了什么,其他服务工作正常。
你能找到代码并告诉我我做错了什么吗?
~src/EventListener/ControllerListener.php
<?php
namespace App\EventListener;
use App\Entity\User;
use App\Entity\DailyWin;
use App\Controller\DailyWinController;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class ControllerListener extends Controller implements DailyWinController
{
private $authChecker, $user, $logger;
public function __construct(UserInterface $user, AuthorizationCheckerInterface $authChecker, LoggerInterface $logger) {
$this->user = $user;
$this->authChecker = $authChecker;
$this->logger = $logger;
}
public function onKernelController(FilterControllerEvent $event) {
if ($this->authChecker->isGranted('IS_AUTHENTICATED_FULLY') === false) return;
$this->logger->warning('Inicjowanie...');
$em = $this->getDoctrine()->getManager();
$DWrep = $em->getRepository(DailyWin::class);
$userId = $this->user->getId();
$userDailyWin = $DWrep->findOneBy(['userId' => $userId]);
$this->logger->warning(print_r($userDailyWin));
if (empty($userDailyWin)) {
$currentDate = new \DateTime(date('Y-m-d', time()));
$userDailyWin = new DailyWin();
$userDailyWin->setUserId($userId);
$userDailyWin->setMultiplier(0);
$userDailyWin->setClaimed(0);
$userDailyWin->setStreak(0);
$userDailyWin->setLastLogin($currentDate);
$em->persist($userDailyWin);
$em->flush();
}
}
}
谢谢。
用户不是 symfony 服务,它不会被自动装配。
试试这个界面:
Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
然后你做:
$user=$tokenStorage->getToken()->getUser();
从 Symfony 3.2 开始,引入了一个新的用户解析器。它似乎不允许通过构造函数注入(我也试图使它起作用),但你应该注入你的方法。
public function onKernelController(UserInterface $user , FilterControllerEvent $event) {
聚会迟到了,但在控制器中你也可以只使用:
$user = $this->getUser();
从 Symfony 3.2 开始,参见 here
当我将 UserInterface 添加到我的事件侦听器时,出现以下错误:
Cannot autowire service "App\EventListener\ControllerListener": argument "$user" of method "__construct()" references interface "Symfony\Component\Security\Core\User\UserInterface" but no such service exists. It cannot be auto-registered because it is from a different root namespace.
我不知道发生了什么,其他服务工作正常。
你能找到代码并告诉我我做错了什么吗?
~src/EventListener/ControllerListener.php
<?php
namespace App\EventListener;
use App\Entity\User;
use App\Entity\DailyWin;
use App\Controller\DailyWinController;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class ControllerListener extends Controller implements DailyWinController
{
private $authChecker, $user, $logger;
public function __construct(UserInterface $user, AuthorizationCheckerInterface $authChecker, LoggerInterface $logger) {
$this->user = $user;
$this->authChecker = $authChecker;
$this->logger = $logger;
}
public function onKernelController(FilterControllerEvent $event) {
if ($this->authChecker->isGranted('IS_AUTHENTICATED_FULLY') === false) return;
$this->logger->warning('Inicjowanie...');
$em = $this->getDoctrine()->getManager();
$DWrep = $em->getRepository(DailyWin::class);
$userId = $this->user->getId();
$userDailyWin = $DWrep->findOneBy(['userId' => $userId]);
$this->logger->warning(print_r($userDailyWin));
if (empty($userDailyWin)) {
$currentDate = new \DateTime(date('Y-m-d', time()));
$userDailyWin = new DailyWin();
$userDailyWin->setUserId($userId);
$userDailyWin->setMultiplier(0);
$userDailyWin->setClaimed(0);
$userDailyWin->setStreak(0);
$userDailyWin->setLastLogin($currentDate);
$em->persist($userDailyWin);
$em->flush();
}
}
}
谢谢。
用户不是 symfony 服务,它不会被自动装配。
试试这个界面:
Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
然后你做:
$user=$tokenStorage->getToken()->getUser();
从 Symfony 3.2 开始,引入了一个新的用户解析器。它似乎不允许通过构造函数注入(我也试图使它起作用),但你应该注入你的方法。
public function onKernelController(UserInterface $user , FilterControllerEvent $event) {
聚会迟到了,但在控制器中你也可以只使用:
$user = $this->getUser();
从 Symfony 3.2 开始,参见 here