【问题标题】:Symfony - There is no extension able to load the configuration - custom configSymfony - 没有扩展能够加载配置 - 自定义配置
【发布时间】:2015-10-28 16:38:15
【问题描述】:

直升机,

我正在尝试将自定义配置加载到我的 AppBundle 中。不幸的是,我得到了:

[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException] 没有扩展能够加载“app”的配置(在 /var/www
/dev.investmentopportunities.pl/src/AppBundle/DependencyInjection/../Resour ces/config/general.yml)。寻找命名空间“app”,没有找到

有几个与此错误相关的类似主题。我查看了它们,但找不到任何可以解决此问题的解决方案。

我的配置文件如下所示:

<?php
namespace AppBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('app');

         $rootNode
            ->children()
            ->arrayNode('tags')
            ->prototype('array')
                ->children()
                    ->scalarNode('name')->isRequired()->end()
                    ->scalarNode('role')->isRequired()->end()
                    ->scalarNode('priority')->isRequired()->end()
                    ->scalarNode('label')->isRequired()->end()
                ->end()
            ->end();

        return $treeBuilder;
    }
}

general.yml:

app:
    tags:
        Accepted:
            name: "Accepted"
            role: "ROLE_ACCEPTTAG"
            priority: "3"
            label: "label label-info"
        Booked:
            name: "Booked"
            role: "ROLE_ACCOUNTANT"
            priority: "3"
            label: "label label-info"
        Finalized:
            name: "Booked"
            role: "ROLE_ACCEPTDOC"
            priority: "1"
            label: "label label-success"

AppExtension.php:

<?php
namespace AppBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Validator\Tests\Fixtures\Entity;

/**
 * This is the class that loads and manages your bundle configuration
 */
class AppExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $container->setParameter('app', $config['app']);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('general.yml'); # another file of yours

    }
}

【问题讨论】:

    标签: symfony


    【解决方案1】:

    TL;DR:你不应该在扩展中加载你的 general.yml 文件。

    为捆绑包定义配置是关于捆绑包将如何处理配置,了解来自config.yml的配置。

    所以你应该在config.yml 中导入你的general.yml 文件,它应该可以工作。

    注意:加载器来自Symfony\Component\DependencyInjection\Loader命名空间,它们用于依赖注入,以允许捆绑包定义服务,主要由第三方捆绑包使用。

    【讨论】:

      猜你喜欢
      • 2016-07-03
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 2013-09-19
      • 1970-01-01
      • 2018-04-22
      • 2019-08-12
      相关资源
      最近更新 更多