gpt4 book ai didi

php - 交响乐 4 : Event listener Cannot autowire UserInterface

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:07:33 26 4
gpt4 key购买 nike


当我将 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 服务,它不会 Autowiring 。

试试这个界面:

Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface

然后你做:

$user=$tokenStorage->getToken()->getUser();

关于php - 交响乐 4 : Event listener Cannot autowire UserInterface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47964172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com