简体   繁体   English

每个包的参数.yml,symfony2

[英]A parameters.yml per bundle, symfony2

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. 我想知道是否可以为每个捆绑包定义一个parameters.yml文件,或者仅为需要它的捆绑包定义并加载它们。

I have searched a lot but i can't find such solution. 我搜索了很多,但我找不到这样的解决方案。

You should clarify a bit; 你应该澄清一下; you want every single bundles to automatically include a parameters.yml file? 您希望每个捆绑包自动包含一个parameters.yml文件吗? I am afraid you would need to modify Symfony's DI core. 我担心你需要修改Symfony的DI核心。 There is an easy alternative though. 虽然有一个简单的选择。

If you create your own bundle with some DependencyInjection then you can add $loader->load('parameters.yml'); 如果您使用一些DependencyInjection创建自己的包,那么您可以添加$loader->load('parameters.yml'); in the bundle's extension class. 在bundle的扩展类中。

The extension class should be located in YourBundle/DependencyInjection/YourBundleExtension.php . 扩展类应位于YourBundle/DependencyInjection/YourBundleExtension.php

The class should look like the following 该类应如下所示

class YourBundleExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    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');
        $loader->load('parameters.yml'); // Adding the parameters file
    }
}

So in this case the parameters.yml file would be in YourBundle/Resources/config/parameters.yml . 所以在这种情况下, parameters.yml文件将在YourBundle/Resources/config/parameters.yml

If you just want to include a parameters file from a bundle into your configuration, just follow the normal format for importing extra configuration files from bundles (the default app shows how to do this with routing). 如果您只想将捆绑包中的参数文件包含到配置中,只需按照常规格式从捆绑包中导入额外的配置文件(默认应用程序显示如何通过路由执行此操作)。 Update your config to include your parameters file like this: 更新您的配置以包含您的参数文件,如下所示:

imports:
    - { resource: parameters.yml }
    - { resource: @YourBundle/Resources/config/parameters.yml }
    - { resource: security.yml }
    - ...

Your parameters file should follow the same format as the default one: 您的参数文件应采用与默认格式相同的格式:

parameters:
    my.parameter: "my value"

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

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