Php 带有自定义配置yaml文件的Symfony 2
我已经在我的项目中创建了一个新的bundle和,并按照上的说明进行了操作 这部分工作正常,但当我尝试添加一个以我的bundle命名的新yaml文件时,我可能会在其他bundle config文件夹中创建类似的命名文件,并使用相同的标记将它们合并到bundles config中,并将其添加到扩展类中以进行加载,我会遇到错误:Php 带有自定义配置yaml文件的Symfony 2,php,symfony,configuration,bundle,Php,Symfony,Configuration,Bundle,我已经在我的项目中创建了一个新的bundle和,并按照上的说明进行了操作 这部分工作正常,但当我尝试添加一个以我的bundle命名的新yaml文件时,我可能会在其他bundle config文件夹中创建类似的命名文件,并使用相同的标记将它们合并到bundles config中,并将其添加到扩展类中以进行加载,我会遇到错误: InvalidArgumentException: There is no extension able to load the configuration for "foo
InvalidArgumentException: There is no extension able to load the configuration for "foo_bar_notify" (in /path/to/symfony/src/Foo/Bar/NotifyBundle/DependencyInjection/../Resources/config/notify.yml). Looked for namespace "foo_bar_notify", found none
请有人指出我错过了什么或做错了什么
代码片段;
配置类
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('foo_bar_notify');
$rootNode
->children()
->arrayNode('roles')
->requiresAtLeastOneElement()
->prototype('array')
->children()
->scalarNode('name')
->isRequired(true)
->end()
->scalarNode('description')
->end()
->arrayNode('placeholders')
->prototype('scalar')->end()
->end()
->booleanNode('html')
->isRequired()
->defaultValue(true)
->end()
->scalarNode('template')
->isRequired(true)
->end()
->end()
->end()
->end()
->end();
return $treeBuilder;
}
扩展类
public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load('foo_bar_notify.yml');
$container->setParameter('notify', $config);
}
public function getAlias()
{
return 'foo_bar_notify';
}
主配置文件
foo_bar_notify:
roles:
software_developer:
name: Alice
description: Foo
placeholders:
- $name
- $email
html: true
template: 'some/path'
super_developer:
name: Malfoy
description: bar
html: false
template: 'some/path/over/here'
自定义yaml notify.yml
foo_bar_notify:
roles:
core_developer:
name: core
html: true
template: 'some/path'
我已经得到了一个解决方案使用的答案 我的扩展类现在看起来像这样
private $configFileName = 'notify.yml';
public function load(array $configs, ContainerBuilder $container)
{
// see if there are any other notify.yml files defined in other bundles
$finder = new \Symfony\Component\Finder\Finder();
$finder
->directories()
->in($container->getParameter('kernel.root_dir') . '/../src/Foo/Bar/*Bundle/Resources')
->path('config');
foreach ($finder as $c_dir) {
$absolute_path = $c_dir.'/'.$this->configFileName;
if (file_exists($absolute_path)) {
$to_add = array_values(Yaml::parse(file_get_contents($absolute_path)));
$configs[0] = $this->merge($configs[0], $to_add[0]);
}
}
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
// set the processed values as a parameter
$container->setParameter('notify', $config);
}
你好我问了这个相关的问题,并找到了更好的解决方案: