【问题标题】:There is no extension able to load the configuration没有扩展能够加载配置
【发布时间】:2016-07-03 03:22:34
【问题描述】:

我正在尝试加载自定义配置但出现异常:

YamlFileLoader.php 中的 1/2 InvalidArgumentException

没有扩展能够加载配置 “cwiczenia”(在 ..\app/config\config.yml 中)。看了 对于命名空间“cwiczenia”,找到“框架”, “安全”,...

2/2 FileLoaderLoadException

没有扩展能够加载“cwiczenia”的配置...

..\src\CwiczeniaDependencyInjectionBundle\DependencyInjection\Configuration.php

namespace CwiczeniaDependencyInjectionBundle\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

class Configuration implements ConfigurationInterface{

public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('cwiczenia');
    
    $rootNode
        ->children()
            ->scalarNode('teamName')->end()
        ->end();
            
    return $treeBuilder;

..\src\CwiczeniaDependencyInjectionBundle\DependencyInjection\CwiczeniaExtension.php

namespace CwiczeniaDependencyInjectionBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;

class CwiczeniaExtension extends Extension
{
    protected function load(array $configs, ContainerBuilder $container) 
    {
        $configuration = $this->getConfiguration($configs, $container);
        $config = $this->processConfiguration($configuration, $configs);
        
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
    public function getAlias()
    {
        return 'cwiczenia';
    }

..\app\config\config.yml

cwiczenia:
    teamName: Lakers

AppKernel

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new AppBundle\AppBundle(),
            new SandersBundle\SandersBundle(),
            new CwiczeniaDependencyInjectionBundle\CwiczeniaDependencyInjectionBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
}

如果我删除 Configuration.php 会出现同样的异常

【问题讨论】:

    标签: symfony


    【解决方案1】:

    你必须手动注册扩展类,这里描述了如何做到这一点http://symfony.com/doc/current/cookbook/bundles/extension.html#manually-registering-an-extension-class

    类似这样的:

    //.....
    class CwiczeniaDependencyInjectionBundle extends Bundle
    {
    
      public function getContainerExtension()
      {
        if (null === $this->extension) {
            $this->extension = new CwiczeniaExtension();
        }
        return $this->extension;
      }
    }
    

    【讨论】:

      【解决方案2】:

      我有同样的问题,并解决它。

      要注册一个扩展,你需要为 bundle 和你的扩展具有相同的名称。所以,如果你想启用你的扩展为cwiczenia,你的扩展文件必须命名为CwiczeniaExtention,并且bundle必须命名为CwiczeniaBundle

      或者,如果您不想更改 Bundle 名称 (CwiczeniaDependencyInjectionBundle),则必须将扩展名重命名为 CwiczeniaDependencyInjectionExtension,然后才能将配置加载为 cwiczenia_dependency_injection

      或者你可以手动注册扩展,正如上面@helios所说的

      【讨论】:

      • 我发现这是一个更好的答案,因为自动注册要好得多。我正在遵循所有步骤,正确的包名称、配置、配置根目录和包应该自动加载,但在我的情况下,没有一个包正在加载。我只是想知道问题是什么。此处的要点包括我运行 behat gist.github.com/bizmate/82b4694d53ac23eac315a032bf41e6a5 时的所有详细信息和错误
      • @Bizmate 首先你的 config.yml 应该是:yml my_reviews_reviews: google: places_key: '%env(GOOGLE_PLACES_KEY)%' yelp: key: '%env(YELP_FUSION_KEY)%' cache: expire: 604800 # 2 days enabled: true symfony.com/doc/current/service_container/… > 使用的 .符号只是一个 Symfony 约定,使参数更易于阅读。参数只是平面键值元素,它们不能组织成嵌套数组 PS。抱歉,降价不可用
      • 问题是我在 services.yml 中加载了特定于包的配置,我通过 YAML 加载器在 Extension 类中加载。我已经使用 flex 重新开始,现在我已经移植了所有内容,并且看起来运行良好
      猜你喜欢
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 2013-09-19
      • 1970-01-01
      • 2018-04-22
      • 2019-08-12
      • 1970-01-01
      相关资源
      最近更新 更多