php - 没有能够加载配置的扩展...查找命名空间 "...",没有找到

标签 php symfony

简介
我知道。关于这个确切主题有成千上万的帖子,但不知何故我仍然无法解决这个问题。我总是收到错误

没有扩展能够加载“第一”的配置(在/var/www/app/src/tzfrs/SpotifyBundle/DependencyInjection/../Resources/config/settings.yml)。查找命名空间“first”,没有找到

目的
我创建了一个包并想要自定义配置选项,但由于它们比普通文件大,我不想将它放在 config.yml 中,而是放在我自己的文件中。

描述
我的捆绑项目结构如下所示 /src/tzfrs/SpotifyBundle

在包中我创建了文件

  • ./TzfrsSpotifyBundle.php
  • ./DependencyInjection/Configuration.php
  • ./DependencyInjection/TzfrsSpotifyExtension.php
  • ./Resources/config/settings.yml

当然,我已经在 AppKernel 中注册了 Bundle,到目前为止,除了我要添加的新配置外,该 bundle 一切正常

TzfrsSpotifyBundle 的内容

<?php
namespace tzfrs\SpotifyBundle;

use tzfrs\SpotifyBundle\DependencyInjection\TzfrsSpotifyExtension;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class TzfrsSpotifyBundle extends Bundle
{
}

/DependencyInjection/Configuration.php 的内容(仅最小化相关信息)

public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('first');

    $rootNode
        ->children()
            ->booleanNode('secondary')->defaultTrue()->end()
        ->end();

    return $treeBuilder;
}

./DependencyInjection/TzfrsSpotifyExtension.php 的内容(仅最小化相关信息)

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'));
    $loader->load('settings.yml');
}

./Resources/config/settings.yml 的内容

first:
    secondary: false

想法
如果出现更多问题或建议行不通,我将使用信息更新此部分


最佳答案

确保您的所有内容都拼写正确,并且您的扩展类确实被调用了。如果您没有正确命名扩展名,那么它将不会被加载。我刚刚对此进行了测试,它运行良好:

cerad_spotify:
    first:
        secondary: false

class CeradSpotifyBundle extends Bundle
{
}

namespace Cerad\SpotifyBundle\DependencyInjection;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('cerad_spotify');
        $rootNode
            ->children()
                ->arrayNode('first')
                    ->children()
                        ->booleanNode('secondary')->end()
                    ->end()
                ->end() // first
            ->end()
        ;
        return $treeBuilder;
    }
}

namespace Cerad\SpotifyBundle\DependencyInjection;

class CeradSpotifyExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();

        $config = $this->processConfiguration($configuration, $configs);

        VarDumper::dump($config);
     }
}

关于php - 没有能够加载配置的扩展...查找命名空间 "...",没有找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39746702/

相关文章:

php - 无法在 Monolog 的 yaml 配置中为 native_mailer 设置 content_type?

parameters - 交响乐, Twig : Persistent route parameters

php - 使用 PHP foreach 迭代 PHP 数组以创建 Javascript 数组

php - 用 PHP 中的数组元素替换准备好的语句占位符

php - 为什么 PHP 的 preg_quote 会转义不必要的字符?

php - 如何将additionProvider与存储库功能一起使用?

symfony - Twig 中的包含和阻止有什么区别?

symfony - 手动身份验证检查 Symfony 2

PHP HTML DOM 解析器从 img 标签中选择 alt

PHPUnit 和 Composer : [InvalidArgumentException] Command "path/to/test" is not defined