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

Php 加载我的捆绑包时,Symfony容器没有扩展,php,symfony,configuration,Php,Symfony,Configuration,我有一个包,它在一段时间内运行得很好。但是,我必须向其添加一些自定义配置参数,因此我在bundle的config.yml中编写了一些行,如下所示: # ... acme_my_bundle: special_params: ['param_1', 'param_2'] 该配置在捆绑包的配置类中定义: namespace ACME\MyBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\

我有一个包,它在一段时间内运行得很好。但是,我必须向其添加一些自定义配置参数,因此我在bundle的config.yml中编写了一些行,如下所示:

# ...
acme_my_bundle:
    special_params: ['param_1', 'param_2']
该配置在捆绑包的
配置
类中定义:

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;
    }
}
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');
    }
}
捆绑包已在
AppKernel.php
中正确注册:

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

    // ...

    return $bundles;
}
但是,当我尝试使用我的应用程序时,我遇到了一个错误:

没有扩展能够加载“acme\u my\u bundle”(在(path\u to\u bundle)/MyBundle/DependencyInjection/./Resources/config/config.yml中)的配置。查找名称空间“acme\u my\u bundle”,未找到任何名称空间
我查找了它,但大多数结果都不令人满意-我消除了搜索过程中出现的问题:

  • 配置结构不当
  • 捆绑包未在应用程序内核中注册
  • 配置根节点名称与从
    ACMEMyBundleExtension::getAlias()返回的名称不同
我尝试调试引发异常的原因,发现当YAML文件加载程序尝试验证我的配置文件时,容器没有扩展名:

var_dump($container->getExtensions()); // prints empty array - array(0) { }
它会导致验证失败,并显示消息的“无”部分-没有可用的扩展名

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

我更改了类的名称等。为了不暴露公司代码,请原谅,如果这会引起混淆

EDIT:我没有明确提到它,但是定义了
扩展
类,并且在加载时会发生异常-正如我上面所写的:

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

更清楚地说,这里是我的
扩展
类:

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

正如这里所解释的,您错过了bundle扩展类(ACME\MyBundle\DependencyInjection\ACMEMyExtension)。捆绑包配置的Cookbook条目为。config.yml中的键应仅命名为acme_my。

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

阅读烹饪书中的更多内容:

[Configuration]类现在可以在load()方法中用于合并配置和强制验证(例如,如果传递了附加选项,将引发异常)

namespace Acme\MyBundle\DependencyInjection;
使用Symfony\Component\HttpKernel\DependencyInjection\Extension;
使用Symfony\Component\DependencyInjection\ContainerBuilder;
类AcmeMyBundleExtension扩展了扩展
{
公共函数加载(数组$configs,ContainerBuilder$container)
{
$configuration=新配置();
$config=$this->processConfiguration($configuration,$configs);
// ...
}
}

根据约定命名扩展类将使其自动加载。有关在中创建DIC扩展类的详细信息。您还可以手动启用扩展,请参阅。

$rootNode=$treeBuilder->root('BUNDLE\CONFIG\u KEY')的
ACME\MyBundle\DependencyInjection\configuration
中检查您的配置读取器

BUNDLE\u CONFIG\u键应为:

  • 有效(在
    ACME\MyBundle\DependencyInjection\Configuration
    和您的
    config.yml
  • 对于应用程序来说是唯一的

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

扩展名已经定义,我已经更新了我的问题,特别提到它,根据我回答中的链接,尝试更改命名约定。ACMEMyBundleExtension到ACMEMyExtension,acme\u my bundle到acme\u my。我认为你的扩展名Symfony由于某些原因没有加载sion。
扩展名
已经定义,我已经更新了我的问题,特别提到了它。请验证您的命名,或者尝试手动启用扩展名:就是这样!我在本地捆绑包
config.yml
中添加了配置,这是一个多么愚蠢的错误。。。