Symfony2。如何在捆绑包中创建多个配置文件
我就是搞不懂 我正在用Symfony2编写一个报告生成器 我有这样一个配置文件:Symfony2。如何在捆绑包中创建多个配置文件,symfony,config,Symfony,Config,我就是搞不懂 我正在用Symfony2编写一个报告生成器 我有这样一个配置文件: bundle: sections: Report1: buckets: bucket1: ... calculations: calculation1: ... report: rows: row1: ... public function get
bundle:
sections:
Report1:
buckets:
bucket1:
...
calculations:
calculation1:
...
report:
rows:
row1:
...
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('bundle');
$rootNode
->children()
->arrayNode('sections')->isRequired()
->prototype('array')
...
对于一些报道来说,这是一个很好的例子
我试着把这个文件分成更小的文件,然后分别加载。这不管用。以下是我尝试过的:
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('bundle_report.yml');
$loader->load('bundle_report_1.yml'); // Order is important here
$loader->load('bundle_report_2.yml');
$loader->load('bundle_report_3.yml');
$loader->load('services.yml');
}
最好的方法是什么?有可能吗
我得到的错误是(在$loader->load()发生之前引发异常):
如果我先切换顺序($loader->load()s,然后切换新配置()):
配置如下所示:
bundle:
sections:
Report1:
buckets:
bucket1:
...
calculations:
calculation1:
...
report:
rows:
row1:
...
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('bundle');
$rootNode
->children()
->arrayNode('sections')->isRequired()
->prototype('array')
...
您不应该从load方法加载配置文件。 这是为了
- 从配置类加载基本配置
- 将其与自定义配置(app/config/中的yml文件)进行比较
- 并加载捆绑服务
The child node "sections" at path "bundle" must be configured
如果您的报告正在发展,那么最好将其放在业务逻辑(模型/实体)中,并插入管理员(例如:sonata admin)。以下是我所做的
// Parse our configuration. It's included in /app/config/config.yml
// Parser is Configuration class in this package.
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
// Great. Parsed. Now copy to parameters for later use.
$container->setParameter('bundle.default_config', $config['default_config']);
$container->setParameter('bundle.sections', $config['sections']);
$container->setParameter('bundle.sections');
在很多搜索中,这是我能想到的最好的。如果你有其他建议,请分享