我正在构建一个扩展来从所有已安装的包中加载配置文件。
我的扩展看起来像这样:
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');
}
}
然后是我的(非常简单的)配置类:我将来会添加一个更复杂的树,但现在我希望它能与这个一起工作:
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;
}
}
为了测试,我只在当前的 MenuBundle\Resources\config 中放置了一个“menu.yml”文件,内容如下:
# file: src\Acme\MenuBundle\Resource\config\menu.yml
mw_menu: "some teststring"
对于手动测试,我在默认 Controller 中使用了一个 testAction,它实际上除了渲染一个空模板外什么都不做。
无论如何,我得到这个错误:
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
我该如何解决?
据我目前的了解,Symfony 注意到配置文件中的键“mw_menu”,但无法找到匹配的配置。
我按照食谱文章工作:How to expose a semantic configuration?
请注意:我知道 Knp Menu Bundle 和类似的 Bundle,但我确实想自己实现一个菜单。
另外我发现this注意。他是对的吗?
最佳答案
那是因为你的代码:
$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
关于php - Symfony2 : There is no extension able to load the configuration for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16122641/