在负载配置中被DI忽略的Symfony

时间:2020-02-26 13:56:06

标签: symfony dependency-injection

起初,我的配置文件如下所示:

# config/services.yaml

services:
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means fetching services directly from the container via $container->get() won't work. The best practice is to be explicit about your dependencies anyway.

    App\:
        resource: '../src/*'
        exclude: '../src/{DBAL/*,Migrations,Form,Tests,Kernel.php,Entity/BaseEntity.php,Repository/BaseRepository.php}'

    App\EventDispatcher\Event\Api\EntityEvent:
        class: App\EventDispatcher\Event\Api\EntityEvent
        public: true

我将一些配置转移到另一个文件:

# config/services.yaml

imports:
    - { resource: 'services/new_config_file.yaml' }   # <------ NEW

services:
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means fetching services directly from the container via $container->get() won't work. The best practice is to be explicit about your dependencies anyway.

    App\:
        resource: '../src/*'
        exclude: '../src/{DBAL/*,Migrations,Form,Tests,Kernel.php,Entity/BaseEntity.php,Repository/BaseRepository.php}'

    # THIS BLOCK IS MOVED
    # App\EventDispatcher\Event\Api\EntityEvent:
    #    class: App\EventDispatcher\Event\Api\EntityEvent
    #    public: true

在新的配置文件中:

# config/services/new_config_file.yaml

services:
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means fetching services directly from the container via $container->get() won't work. The best practice is to be explicit about your dependencies anyway.

    App\:
        resource: '../../src/*'
        exclude: '../../src/{DBAL/*,Migrations,Form,Tests,Kernel.php,Entity/BaseEntity.php,Repository/BaseRepository.php}'


    App\EventDispatcher\Event\Api\EntityEvent:
       class: App\EventDispatcher\Event\Api\EntityEvent
       public: true

结果是,我得到一个错误:

{
    "status": "error",
    "message": "The controller for URI \"/api/broker/\" is not callable. The \"App\\EventDispatcher\\Event\\Api\\EntityEvent\" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead."
}

我了解只能导入参数?在源代码中,它检查是否应该有一个服务密钥

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。即,在深入研究来源symfony-configsymfony-yaml

之后

其导入算法如下: 首先,导入所有文件(满足导入构造) 然后,将解析最终的配置文件本身。

我在哪里错了? 我在行中弄错了

App\:
    resource: '../../src/*'
    exclude: '../../src/{DBAL/*,Migrations,Form,Tests,Kernel.php,Entity/BaseEntity.php,Repository/BaseRepository.php}'

必须将其导入第一个文件(在我的情况下,是在config / services / new_config_file.yaml文件中)。我在每个文件中都导入了这一行 因此,发生了以下情况:

首先导入了服务文件(config / services / new_config_file.yaml)。 就我而言,是这样的:

App\EventDispatcher\Event\Api\EntityEvent:
    class: App\EventDispatcher\Event\Api\EntityEvent
    public: true

然后在config / services.yaml文件中 完成了:

App\:
    resource: '../src/*'
    exclude: '../src/{DBAL/*,Migrations,Form,Tests,Kernel.php,Entity/BaseEntity.php,Repository/BaseRepository.php}'

她磨损了我之前需要的服务导入,因为resource: '../src/*'仅使用public: true重新导入了此服务,在此我们默认进行了描述:

services:
    _defaults:
        ...
        public: false

小心!

相关问题