[英]Cannot autowire service ... Though service parameter is configure
I have a problem autowiring some parameters in services config.我在服务配置中自动装配某些参数时遇到问题。
I can't clear:cache
because it triggers the error so I don't think it is a cache issue.我无法
clear:cache
,因为它会触发错误,所以我认为这不是缓存问题。 And I have copy/paste all my files into a whole new Symfony project and still got the same problem.我已将所有文件复制/粘贴到一个全新的 Symfony 项目中,但仍然遇到同样的问题。
This is my services.yaml这是我的服务。yaml
#config/services.yaml
parameters:
app.medipim_api_key_value: '%env(resolve:MEDIPIM_API_KEY)%'
app.medipim_api_key_id: 326
services:
[6 lines ...]
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude:
'../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Services\MedipimApi:
arguments:
$medipim_api_key_id: '%app.medipim_api_key_id%'
$medipim_api_key_value: '%app.medipim_api_key_value%'
[10 lines ...]
This is my services:这是我的服务:
#src/Services/MedipimApi.php
class MedipimApi
{
const FILTER_CNK_AND_ACTIVE = 'filter_cnk_and_active';
/**
* @var MedipimApiV3Client
*/
private MedipimApiV3Client $medipimApiV3Client;
private TranslatorInterface $translator;
/**
* MedipimApi constructor.
* @param string $medipim_api_key_id API key id for Medipim API call
* @param string $medipim_api_key_value Api key (secret) value for Medipim API call
* @param TranslatorInterface $translator TranslatorInterface object for I18N purpose
*/
public function __construct(string $medipim_api_key_id, string $medipim_api_key_value, TranslatorInterface $translator)
{
$this->translator = $translator;
$this->medipimApiV3Client = new MedipimApiV3Client($medipim_api_key_id, $medipim_api_key_value);
}
....
When I try to call this service, I got this error:当我尝试调用此服务时,出现以下错误:
Cannot autowire service "App\Services\MedipimApi": argument "$medipim_api_key_id" of method "__construct()" is type-hinted "string", you should configure its value explicitly.
无法自动装配服务“App\Services\MedipimApi”:方法“__construct()”的参数“$medipim_api_key_id”是类型提示“string”,您应该明确配置它的值。
It is working only when I copy the value of services.yaml
and paste it into my services_env.yaml
(where env is my current environment).只有当我复制
services.yaml
的值并将其粘贴到我的services_env.yaml
(其中 env 是我当前的环境)时,它才有效。
#config/services_dev.yaml
parameters:
app.medipim_api_key_value: '%env(resolve:MEDIPIM_API_KEY)%'
app.medipim_api_key_id: 326
services:
[6 lines ...]
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Services\MedipimApi:
arguments:
$medipim_api_key_id: '%app.medipim_api_key_id%'
$medipim_api_key_value: '%app.medipim_api_key_value%'
[10 lines ...]
Now it works!现在可以了!
Why did that work?为什么会这样?
Symfony is suppose to load file as described here : Symfony 假设按此处所述加载文件:
For the dev environment, Symfony loads the following config files and directories and in this order:
对于开发环境,Symfony 按以下顺序加载以下配置文件和目录:
- config/packages/*
配置/包/*
- config/packages/dev/*
配置/包/开发/*
- config/services.yaml
配置/服务.yaml
- config/services_dev.yaml
配置/services_dev.yaml
What did I miss?我错过了什么?
EDIT编辑
My kernel class looks right.我的 kernel class 看起来是对的。 It is loading services.yaml and then services_env.yaml as it should be:
它正在加载 services.yaml,然后加载 services_env.yaml,因为它应该是:
class Kernel extends BaseKernel
{
[1 line...]
protected function configureContainer(ContainerConfigurator $container): void
{
$container->import('../config/{packages}/*.yaml');
$container->import('../config/{packages}/'.$this->environment.'/*.yaml');
if (is_file(\dirname(__DIR__).'/config/services.yaml')) {
$container->import('../config/{services}.yaml');
$container->import('../config/{services}_'.$this->environment.'.yaml');
} elseif (is_file($path = \dirname(__DIR__).'/config/services.php')) {
(require $path)($container->withPath($path), $this);
}
}
[14 lines...]
Why you configure param xx_id as int in your yml file?为什么在 yml 文件中将参数 xx_id 配置为 int? In your service you type-hinted id as string !
在您的服务中,您将 id 类型提示为字符串!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.