gpt4 book ai didi

Symfony 升级给我从 4.1 到 4.4 的错误

转载 作者:行者123 更新时间:2023-12-03 20:27:06 24 4
gpt4 key购买 nike

我刚刚从 symfony 4.1 迁移到 4.4
我有这个错误:

Argument 1 passed to App\EventListener\KernelRequestListener::__construct() must be an instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage, instance of Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage given, called in C:\xampp\htdocs\chat-project-symfony\var\cache\dev\Container06Mjwya\srcApp_KernelDevDebugContainer.php on line 1130



如果你看看我的 KernelRequestListener :
<?php

namespace App\EventListener;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
//..

class KernelRequestListener
{
private $tokenStorage;

/**
* KernelRequestListener constructor.
* @param TokenStorage $tokenStorage
* ...
*/
public function __construct(TokenStorage $tokenStorage/*...*/)
{
$this->tokenStorage = $tokenStorage;
//..
}
}

这是我的 config/services.yaml文件:
#...
services:
#..
App\EventListener\KernelRequestListener:
arguments: [ '@security.token_storage' ]
tags:
- { name: kernel.event_listener, event: kernel.request }
- { name: kernel.event_listener, event: kernel.response }

我不知道为什么 symfony 告诉我我正在使用 Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage正在清除写入 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
我已经尝试清除缓存文件夹并删除缓存文件夹,但它没有改变。

我怎样才能解决这个问题 ?

谢谢

最佳答案

I don't know why symfony tell me that I'm using Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage while it's clearing written Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage



这不是 symfony但是 PHP的类型检查功能。你说你的听众想要一个 TokenStorage但是 symfony正在传递给它不同的类,因此错误。

因此,正如@JaredFarrish 指出的那样,您应该使用 TokenStorageInterface在你的构造函数中,像这样:


namespace App\EventListener;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
//..

class KernelRequestListener
{
private $tokenStorage;

/**
* KernelRequestListener constructor.
* @param TokenStorageInterface $tokenStorage
* ...
*/
public function __construct(TokenStorageInterface $tokenStorage/*...*/)
{
$this->tokenStorage = $tokenStorage;
//..
}
}

在存在接口(interface)的地方使用接口(interface)是一种常见的做法,因为这样您将与其他类松散耦合并提供一种对类进行单元测试的方法。

看一看: https://github.com/symfony/security-bundle/blob/master/Resources/config/security.xml#L22他们换了 @security.token_storage的类(class)服务,因为弃用。但是当你使用一个接口(interface)时,你并不关心任何底层的东西,你只知道你将拥有你的方法,因为接口(interface)契约。

关于Symfony 升级给我从 4.1 到 4.4 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59058921/

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