doucang2831 2018-12-11 12:59
浏览 79

Symfony 3.4迁移 - 如何添加捆绑服务? AppExtension与config.yml对比services.yml

I am working on migrating an existing Symfony 2.8 project to Symfony 3.4. The way how the bundle services are imported are different in these Symfony versions and I wonder what are the pros and cons of the different approaches.

The existing project was build using different bundles where each bundles has its own XYBundle\Resources\services.yml file and the XYBundle\DependencyInjection\XYExtension.php file is used to add the service config to the container:

class XYExtension extends Extension {   
    public function load(array $configs, ContainerBuilder $container) {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

This way the services defined in XYBundle\Resources\services.yml become available within the project.

When creating a new Bundle in Symfony 3.4 no extension is created. Instead the services.yml is added to the apps config.yml

// app/config/config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
    - { resource: "@XYBundle/Resources/config/services.yml" }

However, the AppBundle which was created with the Symfony 3.4 skeleton does not have a services.yml file at all. Instead all classes within this bundle are automatically made available as service using autowire and autoconfigure within the app/config/services.yml file.

I am aware that autowire was added in Symfony 3.3 and that it is a great way to automatically add services and add parameters to them. This it is obvious that no XYBundle\Resources\services.yml file is needed any more when autowire is used.

However, is there any difference in using the "old" extension-way to add/import the bundle services.yml file and the new way with importing it in app/config/config.yml?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计