简体   繁体   English

Symfony软件包中的依赖注入和配置

[英]Dependency injection and configuration in a Symfony package

I need to write a package that's going to be used in multiple microservices based on Symfony, and I'm intending to use Symfony 4 as it's released soon. 我需要编写一个软件包,该软件包将在基于Symfony的多个微服务中使用,并且打算使用Symfony 4,因为它即将发布。 Fabien says that it's not recommended to use bundles in Symfony 4 . Fabien说, 不建议在Symfony 4中使用捆绑软件

How do I load the services.yml and config.yml files of my package without using them? 我如何不使用它们加载程序包的services.ymlconfig.yml文件? This article describes how to solve the problem, but it's all about using bundles. 本文介绍了如何解决此问题,但这全都涉及使用捆绑软件。 Or his words do not apply to reusable packages which need to use bundles anyways in order to hook up with Symfony? 还是他的话不适用于可重用的软件包,无论如何它们都需要使用捆绑软件来与Symfony进行连接?

我将服务包装到一个自定义包中,并使用了加载配置的标准方法

u should register it in your src/Kernel.php: 您应该在您的src / Kernel.php中注册它:

protected function build(ContainerBuilder $container): void
{
    $container->registerExtension(new AppExtension());
}

'cause the autoloader from the base Kernel looks only for bundle di folders. 因为基本内核中的自动加载器仅查找bundle di文件夹。 more info: https://symfony.com/doc/current/configuration/using_parameters_in_dic.html 更多信息: https : //symfony.com/doc/current/configuration/using_parameters_in_dic.html

If I understand correctly you want to create something like a stand-alone bundle, so to say. 如果我理解正确的话,可以这么说。

The way I would do it, I would add a config file: config/packages/my_configs.yaml 我这样做的方式是,添加一个配置文件:config / packages / my_configs.yaml

From this location, the file is loaded automatically. 从该位置,将自动加载文件。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM