简体   繁体   English

Symfony2 - 为什么 Symfy 不能加载我的 YAML 配置

[英]Symfony2 - why can't Symonfy load my YAML configuration

I am trying to load a custom YAML config file within a bundle, which errors out, saying:我正在尝试在包中加载自定义 YAML 配置文件,该文件出错,说:

There is no extension able to load the configuration没有能够加载配置的扩展


The custom YAML config file:自定义 YAML 配置文件:

#AppBundle/Resources/config/myconfig.yml
myapp: myvalue

The configuration file:配置文件:

//AppBundle/DependencyInjection/MyConfiguration.php

namespace AppBundle\DependencyInjection;

use ...

class MyConfiguration implements ConfigurationInterface {

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

        return $treeBuilder;
    }

}

The extension file:扩展文件:

//AppBundle/DependencyInjection/AppExtension.php

namespace AppBundle\DependencyInjection;

use ...

class AppExtension extends Extension {

    public function load(array $configs, ContainerBuilder $container)
    {
        $this->processConfiguration(new MyConfiguration(), $configs);

        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__.'/../Resources/config')
        );

        $loader->load('myconfig.yml');
    }   

}

The full error message:完整的错误信息:

InvalidArgumentException in YamlFileLoader.php line 399: There is no extension able to load the configuration for "myapp" (in C:\\my_project\\src\\AppBundle\\DependencyInjection/../Resources/config\\myconfig.yml). YamlFileLoader.php 第 399 行中的 InvalidArgumentException:没有能够加载“myapp”配置的扩展(在 C:\\my_project\\src\\AppBundle\\DependencyInjection/../Resources/config\\myconfig.yml)。 Looked for namespace "myapp", found none寻找命名空间“myapp”,没有找到


This is because you use the myapp custom alias.这是因为您使用了myapp自定义别名。

First solution: use the default alias第一种解决方案:使用默认别名

Symfony uses the underscored version of the bundle name by default (eg AcmeTestBundle will be transformed to acme_test ).默认情况下,Symfony 使用下划线版本的包名称(例如AcmeTestBundle将被转换为acme_test )。

Considering your bundle namespace, the app alias will be the default.考虑到您的包命名空间, app别名将是默认值。

You can change this code:您可以更改此代码:

$treeBuilder->root('myapp')->end();

into this:进入这个:

$treeBuilder->root('app')->end();

And then use the app key in your configuration file.然后在您的配置文件中使用app密钥。

This is the preferred solution, as it makes your configuration match your bundle names.这是首选的解决方案,因为它使您的配置与您的包名称相匹配。 If the configuration name doesn't suit you, maybe it's because the bundle is not named correctly!如果配置名称不适合您,可能是因为捆绑包名称不正确!

Alternative solution: keep your custom alias替代解决方案:保留您的自定义别名

You may have only one bundle in your app, which is called AppBundle because you don't intend to reuse this bundle in any other project.您的应用程序中可能只有一个包,称为AppBundle因为您不打算在任何其他项目中重用此包。 If you still want to use a custom alias ( myapp instead of app ), here is the way to go:如果您仍想使用自定义别名( myapp而不是app ),可以使用以下方法:

<?php
//AppBundle/DependencyInjection/AppExtension.php

namespace AppBundle\DependencyInjection;

use ...

class AppExtension extends Extension {

    public function load(array $configs, ContainerBuilder $container)
    {
        // ...
    }


    public function getAlias()
    {
        return 'myapp';
    }
}

As Symfony will not like this alias by default, change your Bundle file as well:因为 Symfony 默认不喜欢这个别名,所以也要更改你的 Bundle 文件:

<?php
// AppBundle/AppBundle.php
namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;

class AppBundle extends Bundle
{
    /**
     * {@inheritdoc}
     */
    public function getContainerExtension()
    {
        if (null === $this->extension) {
            $extension = $this->createContainerExtension();

            if (null !== $extension) {
                if (!$extension instanceof ExtensionInterface) {
                    throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
                }

                $this->extension = $extension;
            } else {
                $this->extension = false;
            }
        }

        if ($this->extension) {
            return $this->extension;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM