Symfony2:没有可以加载配置的扩展 [英] Symfony2: There is no extension able to load the configuration for
问题描述
我的扩展看起来像这样:
命名空间Acme\MenuBundle\DependencyInjection;
// use ...
use Acme\MenuBundle\DependencyInjection\Configuration;
class AcmeMenuExtension extends Extension {
public function load(array $ configs,ContainerBuilder $ container){
$ configuration = new Configuration();
$ config = $ this-> processConfiguration($ configuration,$ configs);
$ finder = new \Symfony\Component\Finder\Finder();
$ finder
- >目录()
- > in($ container-> getParameter('kernel.root_dir')。'/../src/*/*Bundle / Resources')
- > path('config');
$ possibleLocations = array();
foreach($ finder as $ c_dir){
$ possibleLocations [] = $ c_dir-> getPathName();
}
$ loader = new Loader\YamlFileLoader($ container,new FileLocator($ possibleLocations));
$ loader-> load('menu.yml');
}
}
然后有我的(非常简单的)配置类:将来我会添加一个更复杂的树,但是现在我想要这样做:
命名空间Acme \\MenuBundle\DependencyInjection;
//使用...
类配置实现ConfigurationInterface {
public function getConfigTreeBuilder(){
$ treeBuilder = new TreeBuilder();
$ rootNode = $ treeBuilder-> root('mw_menu');
$ rootNode
- > children()
- > scalarNode('mw_menu') - > defaultValue('') - > end()
- > end();
return $ treeBuilder;
}
}
对于测试,我只放置了一个menu.yml当前MenuBundle\Resources\config中的文件与此内容:
#file:src\Acme\MenuBundle\\ \\Resource\config\menu.yml
mw_menu:some teststring
手动测试我在一个默认的Controller中使用了一个testAction,实际上它只是一个空模板。
无论如何,我得到这个错误:
InvalidArgumentException:没有可以加载
mw_menu的配置的扩展名(在{path to symfony} / app /../ src /Acme/MenuBundle/Resources\config\menu.yml)。
查找命名空间mw_menu,找不到
我如何解决?
从目前的了解中,Symfony注意到配置文件中的mw_menu键,但无法找到匹配的配置。 p>
我在菜谱文章中工作:如何公开语义配置?
请注意:我知道Knp Menu Bundle和类似的软件包,但我想自己实现一个菜单。 / p>
此外,我发现这个通知。他是对的吗?
这是因为你的代码:
$ rootNode = $ treeBuilder-> root('mw_menu');
$ rootNode
- > children()
- > scalarNode('mw_menu') - > defaultValue('') - > end()
- > end();
表示配置文件应如下所示:
mw_menu:
mw_menu:some teststring
一个例子:
更具体地说,您需要将代码调整到所需的任何节点。一般来说,人们使用一般的根,例如 mw_menu
,然后在以下示例中使用次级值,如 database_driver
:
$ rootNode = $ treeBuilder-> root('mw_menu');
$ rootNode
- > children()
- > scalarNode('database_driver') - > defaultValue('orm') - > end()
- > end();
那么在配置文件中会看起来像这样:
mw_menu:
database_driver:mongo_db
I am building an extension to load config files from all installed bundles.
my Extension looks like this:
namespace Acme\MenuBundle\DependencyInjection;
// use ...
use Acme\MenuBundle\DependencyInjection\Configuration;
class AcmeMenuExtension extends Extension {
public function load(array $configs, ContainerBuilder $container) {
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$finder = new \Symfony\Component\Finder\Finder();
$finder
->directories()
->in($container->getParameter('kernel.root_dir') . '/../src/*/*Bundle/Resources')
->path('config');
$possibleLocations = array();
foreach ($finder as $c_dir) {
$possibleLocations[] = $c_dir->getPathName();
}
$loader = new Loader\YamlFileLoader($container, new FileLocator($possibleLocations));
$loader->load('menu.yml');
}
}
And then there is my (very simple) Configuration class: I will add a more complex tree in the future, but for now I want it to work with this:
namespace Acme\MenuBundle\DependencyInjection;
// use ...
class Configuration implements ConfigurationInterface {
public function getConfigTreeBuilder() {
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mw_menu');
$rootNode
->children()
->scalarNode('mw_menu')->defaultValue('')->end()
->end();
return $treeBuilder;
}
}
For testing I only placed a "menu.yml" file inside the current MenuBundle\Resources\config with this content:
# file: src\Acme\MenuBundle\Resource\config\menu.yml
mw_menu: "some teststring"
For manual testing I use a testAction in a default Controller, which does actually nothing but render an empty template.
Anyway, I get this error:
InvalidArgumentException: There is no extension able to load the configuration for
"mw_menu" (in {path to symfony}/app/../src/Acme/MenuBundle/Resources\config\menu.yml).
Looked for namespace "mw_menu", found none
How can I fix that?
From what I currently understand, Symfony notices the key 'mw_menu' in the config file, but is not able to find the matching configuration.
I worked along the cookbook article: How to expose a semantic configuration?
Please note: I know of Knp Menu Bundle and similar Bundles but I do want to implement a menu myself.
Additionally I found this notice. Is he right?
That's because your code:
$rootNode = $treeBuilder->root('mw_menu');
$rootNode
->children()
->scalarNode('mw_menu')->defaultValue('')->end()
->end();
means that the config file should look like this:
mw_menu:
mw_menu: "some teststring"
An example:
To be more specific, you need to adapt your code to whatever node you want. In general, people use a general root, like mw_menu
, and then secondary value like database_driver
in the following example:
$rootNode = $treeBuilder->root('mw_menu');
$rootNode
->children()
->scalarNode('database_driver')->defaultValue('orm')->end()
->end();
Which would then look like this in config file:
mw_menu:
database_driver: mongo_db
这篇关于Symfony2:没有可以加载配置的扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!