每个包一个 parameters.yml,symfony2

2022-01-16 00:00:00 php symfony doctrine

我想知道是否可以为每个包定义一个 parameters.yml 文件,或者只为需要它的包定义并加载它们.

I was wondering if it's possible to defined a parameters.yml file for every bundle or only for the bundles that need it and load those.

我已经搜索了很多,但我找不到这样的解决方案.

I have searched a lot but i can't find such solution.

推荐答案

你应该澄清一下;您希望每个捆绑包都自动包含一个 parameters.yml 文件吗?恐怕您需要修改 Symfony 的 DI 核心.不过有一个简单的替代方案.

You should clarify a bit; you want every single bundles to automatically include a parameters.yml file? I am afraid you would need to modify Symfony's DI core. There is an easy alternative though.

如果您使用一些 DependencyInjection 创建自己的包,那么您可以在包的扩展类中添加 $loader->load('parameters.yml');.

If you create your own bundle with some DependencyInjection then you can add $loader->load('parameters.yml'); in the bundle's extension class.

扩展类应该位于YourBundle/DependencyInjection/YourBundleExtension.php.

该类应如下所示

class YourBundleExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new LoaderYamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
        $loader->load('parameters.yml'); // Adding the parameters file
    }
}

因此,在这种情况下,parameters.yml 文件将位于 YourBundle/Resources/config/parameters.yml 中.

So in this case the parameters.yml file would be in YourBundle/Resources/config/parameters.yml.

相关文章