简体   繁体   English

无法自动装配服务...虽然服务参数已配置

[英]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.

相关问题 无法自动服务 - Cannot autowire service Symfony 5.1 - 无法自动装配服务 - Symfony 5.1 - Cannot autowire service Symfony 无法自动装配服务 不存在此类服务 - Symfony cannot autowire service no such service exists 无法自动装配服务“App\\Service\\MatchCarAdService”:方法的参数“$templating” - Cannot autowire service "App\Service\MatchCarAdService": argument "$templating" of method 无法自动装配服务:参数引用 class 但不存在此类服务 - Cannot autowire service: Argument references class but no such service exists Symfony 4.4:无法自动连接服务“Symfony\Component\Validator\Context\ExecutionContextFactory” - Symfony 4.4: Cannot autowire service "Symfony\Component\Validator\Context\ExecutionContextFactory" 尝试清除 Symfony 4 应用程序缓存时出现“无法自动装配服务”异常 - "Cannot autowire service" exception when trying to clear Symfony 4 application cache 无法自动装配“App\Controller\HomeController”参数 $:它引用 class“Symfony\Component\Form\SubmitButton”但不存在此类服务 - Cannot autowire argument $of "App\Controller\HomeController": it references class "Symfony\Component\Form\SubmitButton" but no such service exists Symfony:无法自动装配“Controller:method()”的参数 $injectedVar:它引用 class“App\Entity\Entity”,但不存在此类服务 - Symfony: Cannot autowire argument $injectedVar of "Controller:method()": it references class "App\Entity\Entity" but no such service exists Symfony4-无法自动连线服务 - Symfony4 - Can't Autowire Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM