代码之家  ›  专栏  ›  技术社区  ›  Andrei Herford

Symfony autowire-不同service.yml文件之间的关系是什么?

  •  0
  • Andrei Herford  · 技术社区  · 7 年前

    我正在迁移现有的 Symfony 2.8 计划 Symfony 3.4 . 在这个过程中,我想使用 autowire 自动配置/设置服务。

    来自SF2,我的项目仍然以不同的捆绑方式组织,因此我使用不同的 services.yml 文件夹。

    服务.yml 服务.yml 文件夹。

    具体问题是:

    // app/config/config.yml
    imports:
        - { resource: "@AppBundle/Resources/config/services.yml" }
        ...
    
    
    // app/config/services.yml
    services:
        _defaults:
            autowire: true
            autoconfigure: true
            public: false
            bind:
                $debug: '%kernel.debug%'
    
    
    // src/AppBundle/Resources/config/services.yml
    services:
        AppBundle\Controller\CustomExceptionController:
            public: true
            #autowire: true
            #arguments:
            #    $debug: '%kernel.debug%'
    
    
    // AppBundle\Controller\CustomExceptionController
    class CustomExceptionController extends ExceptionController {
        public function __construct(\Twig_Environment $twig, $debug) {
            parent::__construct($twig, $debug);
            ....
        }
    }
    

    这个 CustomExceptionController 仅当服务定义为 public _defaults private AppBundle 我(现在)想把这个配置保存在bundle中 service.yml

    public: true :

    类型错误:参数太少,无法运行 传入 …/project/var/cache/dev/ContainerW0i3edc/getCustomExceptionControllerService.php 在第11行和第2行

    尽管消息提到了缓存,但这不是缓存问题(删除文件夹并重新构建缓存不会改变任何事情)。

    除非我加上 autowire: true 对于具体的服务定义,autowire会尝试设置服务。然而,我仍然得到以下信息:

    “AppBundle\Controller\CustomExceptionController”:参数“$debug” 方法“\uuu construct()”的类型没有类型提示,您应该配置其

    $debug 被限制在 app/config/services.yml 这不会是个问题吧? 但是,只有在 src/AppBundle/Resources/config/services.yml .

    自动连线:对 服务.yml ?

    为什么绑定来自项目 服务.yml

    是预期的行为还是我的配置有问题?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Community Mohan Dere    6 年前

    为什么autowire:true不应用于bundle services.yml中定义的服务?

    为什么project services.yml的绑定不应用于bundle services.yml中定义的服务?

    是预期的行为还是我的配置有问题?

    对于您的所有问题:

    _defaults 仅适用于本地定义的服务,不适用于导入的服务 .

    如何解决你的问题?

    添加 src/AppBundle/Resources/config/services.yml 文件


    您可以阅读有关Symfony 3.3+DI更改的更多信息 in documentation 或者在我的 post with before/after examples

    推荐文章