当我想为捆绑包创建配置参数时,我收到了这个错误。
Symfony''Component''DependencyInjection''Exception''InvalidArgumentException]没有扩展能够加载的配置"mrc_morales_tyre"
这是代码:
app/config/config.yml
mrc_morales_tyre:
db_driver: orm
配置.php
namespace MrcMorales'TyreBundle'DependencyInjection;
use Symfony'Component'Config'Definition'Builder'TreeBuilder;
use Symfony'Component'Config'Definition'ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mrc_morales_tyre');
$supportedDrivers = array('orm');
$rootNode
->children()
->scalarNode('db_driver')
->validate()
->ifNotInArray($supportedDrivers)
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers))
->end()
->end()
->end();
return $treeBuilder;
}
}
TyreExtension.php
namespace MrcMorales'TyreBundle'DependencyInjection;
use Symfony'Component'DependencyInjection'ContainerBuilder;
use Symfony'Component'Config'FileLocator;
use Symfony'Component'HttpKernel'DependencyInjection'Extension;
use Symfony'Component'DependencyInjection'Loader;
use Symfony'Component'Config'Definition'Processor;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class TyreExtension extends Extension
{
/**
* {@inheritdoc}
*/
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'));
}
}
TyreBundle.php
namespace MrcMorales'TyreBundle;
use Symfony'Component'HttpKernel'Bundle'Bundle;
use Symfony'Component'DependencyInjection'ContainerBuilder;
use MrcMorales'TyreBundle'DependencyInjection'Compiler'ValidationPass;
class TyreBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ValidationPass());
}
}
ValidationPass是在不常用的文件夹中加载validation.yml,它可以工作。
感谢
已解决:
默认情况下,Symfony期望配置var name是扩展名,在本例中为tire。
然后我需要将扩展名更改为MrcMoralesTyreExtension,但随后我们需要覆盖中的方法getContainerExtension()
TyreBundle.php
public function getContainerExtension()
{
return new MrcMoralesTyreExtension();
}