Merge config files in symfony2

核能气质少年 提交于 2019-12-08 09:50:18

问题


I build config for my bundle using ConfigurationInterface. My extension file look like this:

public function load(array $configs, ContainerBuilder $container) {
    $configuration = new Configuration();
    $config = $this->processConfiguration($configuration, $configs);
    $container->setParameter('my_admin_included_bundles', $config['included_bundles_namespace']);
    $container->setParameter('my_admin_widget_script_file', $config['widget_script_file']);
    $container->setParameter('my_admin_widgets', $config['widgets']);
    $container->setParameter('my_admin_menu', $config['menu']); 
}

public function getAlias() {
    return 'my_admin';
}

I test it out by putting my config params into (global) app/config.yml, and it work's. But i want to putt those parameters in to custom file like MyBundle\Resources\Config\my_config.yml and merge this file with global. Any sugestions?


回答1:


In your app/config/config.yml file, you can import other configuration files (even with other formats):

# app/config/config.yml
import:
    - { resource: path/to/my_config.yml }

However, it's better (it makes your bundle more reusable) if you use prepended configuration. With that, you can set the default values for other bundle's configuration. This way, a User can still override it when they want that.




回答2:


OK. I found a temporary solution to the problem. It looks like this

use Symfony\Component\Yaml\Parser;    

public function load(array $configs, ContainerBuilder $container) {       
            $yaml = new Parser();
            $configs = $yaml->parse(file_get_contents(__DIR__ . '/../Resources/config/config.yml'));

            $configuration = new Configuration();
            $config = $this->processConfiguration($configuration, $configs);
            $container->setParameter('my_cms_widget_script_file', $config['widget_script_file']);
            $container->setParameter('my_cms_layout_list', $config['layout_list']);
            $container->setParameter('my_cms_default_layout', $config['default_layout']);
            $container->setParameter('my_cms_widgets', $config['widgets']);
            $container->setParameter('my_cms_menu', $config['menu']);
        }

I take my custom config.yml, parse file to array, then add as parameter to the container. Any sugestions?



来源:https://stackoverflow.com/questions/20836739/merge-config-files-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!