加载我的捆绑包时,Symfony容器没有扩展名 [英] Symfony container has no extensions when loading my bundle

查看:56
本文介绍了加载我的捆绑包时,Symfony容器没有扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个捆绑包,在一段时间内运作良好。但是,我必须在其中添加一些自定义配置参数,因此我在捆绑包的config.yml中写了几行,就像这样:

I have a bundle which was working quite well for some time. However, I had to add some custom configuration params to it, so I wrote some lines in the bundle's config.yml, something like this:

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

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

The configuration is defined in the bundle's Configuration class:

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;
}

但是,当我尝试使用我的应用程序时,出现错误:

However, when I try to use my app, I get an error:

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

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

I looked it up, but most results found were unsatisfactory - I eliminated the problems that came up during searching:


  • 不正确的配置结构

  • 未在其中注册的捆绑包应用程序内核

  • 配置根节点名称,该名称不同于 ACMEMyBundleExtension :: getAlias()
  • $ b $返回的根节点名称b
  • improper configuration structure
  • bundle not registered in the app kernel
  • config root node name different than the one returned from ACMEMyBundleExtension::getAlias()

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

I tried debugging the cause of the exception being thrown and discovered that when the YAML file loader tries to validate my config file, the container has no extensions:

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

这会导致验证失败,并且没有要显示的消息的一部分-没有可用的扩展名。

It causes the validation to fail and the none part of the message to be displayed - there a no available extensions.

我尝试调试 $ this->。 ContainerBuilder :: hasExtension()中的扩展,由于某种原因,该列表针对供应商捆绑包启动时是完整的,但对于我的包。看来我的捆绑包中的某些东西仍然没有正确定义或注册。

I tried debugging $this->extensions in ContainerBuilder::hasExtension() and for some reason the list is complete when the method is launched for the vendor bundles, but is empty for my bundle. It looks like that something in my bundle is still defined or registered incorrectly.

我从类中更改了名称,等等。不要公开公司代码,对不起

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

I didn't explicitly mention it, but the Extension class is defined and the exception occurs when it is loaded - just as I have written above:


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

when the YAML file loader tries to validate my config file

更清楚地说,这是我的 Extension 类:

To be more clear, here is my Extension class:

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');

Check your configuration reader in ACME\MyBundle\DependencyInjection\Configuration for $rootNode = $treeBuilder->root('BUNDLE_CONFIG_KEY');.

BUNDLE_CONFIG_KEY 应该是:


  • 有效(与 ACME\MyBundle\DependencyInjection\Configuration 和您的 config.yml

  • 应用唯一性

  • valid (same in ACME\MyBundle\DependencyInjection\Configuration and your config.yml
  • unique for the app

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

Also please check you're defining bundle configuration in correct way - it should be added to app/config/*.yml (one of global config files). Maybe you have added acme_my_bundle config in other custom bundle config files?

这篇关于加载我的捆绑包时,Symfony容器没有扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆