php - 加载我的包时 Symfony 容器没有扩展

标签 php symfony configuration

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

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

配置在 bundle 的 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;
    }
}

该 bundle 已在 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

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

  • 配置结构不正确
  • bundle 未在应用内核中注册
  • 配置根节点名称与从 ACMEMyBundleExtension::getAlias() 返回的名称不同

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

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

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

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

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

编辑:我没有明确提及它,但是定义了 Extension 类,并且在加载它时发生异常 - 正如我上面所写的:

when the YAML file loader tries to validate my config file

更清楚地说,这是我的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 (全局配置文件之一)。也许您已在其他自定义 bundle 配置文件中添加了 acme_my_bundle 配置?

关于php - 加载我的包时 Symfony 容器没有扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28937442/

相关文章:

eclipse - 如何在 Eclipse 中更改引用的类提供者?

web - 站点地图中的多个 url 位置

php - 复选框切换全选/取消全选问题

symfony - 奏鸣曲管理员 : specific batch_confirmation template per batch action

symfony - 为什么无法在 CompilerPass 中注入(inject) logger 服务?

php - 生产环境中的 Doctrine 模式更新错误

user-interface - vim + iterm : how to use mouse for everything EXCEPT selection?

php - 如何在没有 GD 的情况下在 PHP 中合并两个图像?

php - $_SERVER ['REQUEST_METHOD' ] 默认计算为 'GET'

php - 打印时 IE 不重新发送 cookie