简体   繁体   English

如何在Symfony2中扩展不同包的配置?

[英]How to extend config of different bundle in Symfony2?

I know I can overwrite templates or extend classes of other bundles. 我知道我可以覆盖模板或扩展其他包的类。 But can I extend also configs? 但我可以延长配置吗? I was hoping to be able to load other namespaces from config in DependenyInjection/AcmeExtension.php 's load method, but I haven't found anything about it anywhere. 我希望能够在DependenyInjection/AcmeExtension.php的加载方法中从config加载其他命名空间,但我在任何地方都没有找到任何关于它的信息。

Example: 例:

I have AcmeBundle which defines following in config: 我有AcmeBundle,它在config中定义了以下内容:

acme:
    a: 1

I want to extend this bundle (in new bundle called AwesomeAcmeBundle) and be able to define another variables either by adding them to original namespace: 我想扩展这个包(在名为AwesomeAcmeBundle的新包中)并能够通过将它们添加到原始命名空间来定义另一个变量:

acme:
    a: 1
    b: 2

or by wrapping original namespace to new one and adding new variables there: 或者通过将原始名称空间包装到新名称空间并在其中添加新变量

awesome_acme:
    a: 1
    b: 2

I had similar needs and I have solved them in the following way: 我有类似的需求,我已通过以下方式解决了这些问题:

1) Extend the parent's Configuration class 1)扩展父的Configuration类

//FooBundle\DependencyInjection\Configuration.php

use DerpBundle\DependencyInjection\Configuration as BaseConfiguration;

class Configuration extends BaseConfiguration
{

    public function getConfigTreeBuilder()
    {
        $treeBuilder = parent::getConfigTreeBuilder();

        //protected attribute access workaround
        $reflectedClass = new \ReflectionObject($treeBuilder);
        $property = $reflectedClass->getProperty("root");
        $property->setAccessible(true);

        $rootNode = $property->getValue($treeBuilder);

        $rootNode
           ->children()
           ...

        return $treeBuilder;
     }
}

2) Create own extension that actually can handle the new configuration entries 2)创建实际可以处理新配置条目的自己的扩展

class FooExtension extends Extension
{

    public function load(array $configs, ContainerBuilder $container)
    {

        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        //custom parameters
        $container->setParameter('new_param_container_name', $config['new_param_name']);

     ...
     }
}

3) in the app\\config\\config.yml you can use in your new foo attribute-set all the parameters that derp (as a parent bundle) has plus any of your new params that you have defined in the Configuration.php . 3)在app\\config\\config.yml你可以在你的新foo属性中使用 - 设置derp (作为父包)的所有参数以及你在Configuration.php定义的任何新参数。

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

If you're talking about the .yml s, you can import the AcmeBundle s confing in the AwesomeAcmeBundle config with 如果你在谈论.yml ,你可以在AwesomeAcmeBundle配置中导入AcmeBundle s confing

imports:
    - { resource: path/to/AcmeBundles/config.yml }

and then overwrite the parameters you want. 然后覆盖您想要的参数。

Symfony is doing the same in the config_dev.yml with the framework/router parameter. Symfony在config_dev.yml使用framework/router参数执行相同的操作。

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

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