2015-02-10 55 views
2

當我想創建配置參數到包時,我獲得了此錯誤。在包中創建配置參數時出錯

的Symfony \元器件\ DependencyInjection \異常\ InvalidArgumentException] 沒有擴展能夠加載配置爲 「mrc_morales_tyre」

這是代碼:

應用/配置/ config.yml

mrc_morales_tyre: 
    db_driver: orm 

Configuration.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和它的作品。

感謝

+0

很奇怪......有你啓用了捆在AppKernel類正確嗎? – Matteo 2015-02-10 14:58:26

+0

是啊!新的MrcMorales \ TyreBundle \ TyreBundle(), – 2015-02-10 15:17:24

+0

你能告訴我們你的Bundle.php文件嗎? (你的包的根源) – Snroki 2015-02-10 16:19:14

回答

0

解決:

Symfony的默認預計比配置變量名稱是擴展的名稱,在這種情況下輪胎。

然後,我需要的擴展名更改爲MrcMoralesTyreExtension但我們需要覆蓋的方法getContainerExtension():

TyreBundle.php

public function getContainerExtension() 
    { 
     return new MrcMoralesTyreExtension(); 
    }