没有能够加载配置的扩展

Sru*_*ons 6 symfony

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

YamlFileLoader.php 中的 1/2 InvalidArgumentException

没有扩展能够加载“cwiczenia”的配置(在 ..\app/config\config.yml 中)。查找命名空间“cwiczenia”,找到“framework”、“security”、...

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;
Run Code Online (Sandbox Code Playgroud)

..\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';
    }
Run Code Online (Sandbox Code Playgroud)

..\app\config\config.yml

cwiczenia:
    teamName: Lakers
Run Code Online (Sandbox Code Playgroud)

应用内核

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我删除 Configuration.php,同样的异常

小智 3

您必须手动注册扩展类,此处描述了如何操作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;
  }
}
Run Code Online (Sandbox Code Playgroud)