Symfony容器在加载我的捆绑包时没有扩展


Symfony container has no extensions when loading my bundle

我有一个捆绑包,它在一段时间内运行良好。但是,我不得不添加一些自定义配置参数,所以我在捆绑包的 config.yml 中写了一些行,如下所示:

# ...
acme_my_bundle:
    special_params: ['param_1', 'param_2']

配置在捆绑包的 Configuration 类中定义:

namespace ACME'MyBundle'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('acme_my_bundle');
        $rootNode
            ->children()
                ->arrayNode('special_params')
                ->end()
            ->end();
        return $treeBuilder;
    }
}

捆绑包已正确注册AppKernel.php

public function registerBundles() {
    $bundles = array(
        // ...
        new ACME'MyBundle(),
        // ...
    );
    // ...
    return $bundles;
}

但是,当我尝试使用我的应用程序时,我收到一个错误:

There is no extension able to load the configuration for "acme_my_bundle" (in (path_to_bundle)/MyBundle/DependencyInjection/../Resources/config/config.yml). Looked for namespace "acme_my_bundle", found none

我查了一下,但发现的大多数结果都不令人满意 - 我消除了搜索过程中出现的问题:

  • 配置结构不当
  • 未在应用程序内核中注册的捆绑包
  • 配置根节点名称与从ACMEMyBundleExtension::getAlias()返回的名称不同

我尝试调试引发异常的原因,发现当 YAML 文件加载程序尝试验证我的配置文件时,容器没有扩展名:

var_dump($container->getExtensions()); // prints empty array - array(0) { }

它会导致验证失败并显示消息的部分 - 没有可用的扩展。

我尝试在ContainerBuilder::hasExtension()调试$this->extensions,由于某种原因,当为供应商捆绑包启动该方法时,列表已完成,但对于我的捆绑包来说为空。看起来我的捆绑包中的某些内容仍然定义或注册不正确。

我更改了类等名称,以免暴露公司代码,如果引起混淆,请原谅我。

编辑:我没有明确提到它,但是定义了Extension类,并且在加载时会发生异常 - 就像我上面写的那样:

当 YAML 文件加载程序尝试验证我的配置文件时

为了更清楚,这是我Extension课:

namespace ACME'MyBundle'DependencyInjection;
use Symfony'Component'DependencyInjection'ContainerBuilder;
use Symfony'Component'Config'FileLocator;
use Symfony'Component'HttpKernel'DependencyInjection'Extension;
use Symfony'Component'DependencyInjection'Loader;
/**
 * 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 ACMEMyBundleExtension 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'));
        // The exception is thrown here
        $loader->load('config.yml');
    }
}

ACME'MyBundle'DependencyInjection'Configuration 中检查您的配置读取器以了解$rootNode = $treeBuilder->root('BUNDLE_CONFIG_KEY');

BUNDLE_CONFIG_KEY应该是:

  • 有效(与ACME'MyBundle'DependencyInjection'Configuration和您的config.yml相同
  • 应用程序的独特性

另外请检查您是否以正确的方式定义捆绑包配置 - 它应该添加到app/config/*.yml(全局配置文件之一)。也许您已经在其他自定义捆绑包配置文件中添加了acme_my_bundle配置?

您错过了捆绑扩展类(ACME''MyBundle''DependencyInjection''ACMEMyExtension),如此处 http://symfony.com/doc/current/cookbook/bundles/extension.html 所述。此处提供了捆绑包配置的说明书条目。config.yml 中的键应仅命名为 acme_my。

仅创建Configuration类是不够的。您需要注册依赖项注入扩展并在其中使用 Configuration 类。

在如何为捆绑包创建友好配置说明书中阅读更多信息:

[配置] 类现在可以在 load() 方法中使用来合并配置和强制验证(例如,如果传递了附加选项,则会抛出异常)

namespace Acme'MyBundle'DependencyInjection;
use Symfony'Component'HttpKernel'DependencyInjection'Extension;
use Symfony'Component'DependencyInjection'ContainerBuilder;
class AcmeMyBundleExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        // ...
    }
}

根据约定命名扩展类将使其自动加载。有关创建 DIC 扩展类的更多信息,请参阅创建扩展类。也可以手动启用扩展,请参阅手动注册扩展类。