【问题标题】:Symfony2's YamlFileLoader: Looked for namespace "mybundle_tools", found noneSymfony2 的 YamlFileLoader:寻找命名空间“mybundle_tools”,没有找到
【发布时间】:2017-08-03 14:00:24
【问题描述】:

编辑:删除 YamlFileLoader 行后,我处理的配置为空(数组(0){}):

我的新扩展

<?php
namespace MyProject\MyBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MyProjectMyExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration,$configs);

        var_dump($config); // array(0) { } 

        /*...*/
    }
}

相关错误

ContextErrorException in MyProjectMyExtension.php line 22: Notice: Undefined index: cachedir

in MyProjectMyExtension.php line 22
at ErrorHandler->handleError('8', 'Undefined index: cachedir', '/home/jeff/Projets/MyProject/src/MyProject/MyBundle/DependencyInjection/MyProjectMyExtension.php', '22', array('configs' => array(array()), 'container' => object(ContainerBuilder), 'configuration' => object(Configuration), 'config' => array())) in MyProjectMyExtension.php line 22
at MyProjectMyExtension->load(array(array()), object(ContainerBuilder)) in MergeExtensionConfigurationPass.php line 59
at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in MergeExtensionConfigurationPass.php line 39
at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in Compiler.php line 104
at Compiler->compile(object(ContainerBuilder)) in ContainerBuilder.php line 598
at ContainerBuilder->compile() in Kernel.php line 514
at Kernel->initializeContainer() in Kernel.php line 133
at Kernel->boot() in Kernel.php line 182
at Kernel->handle(object(Request)) in app_dev.php line 27

看来我的配置还是真的坏了。感谢您的评论;你知道这里出了什么问题吗?


我遇到了 Symfony 2.8 第三方捆绑配置问题。

我正在为我的包创建自定义配置,但 YamlFileLoader 似乎无法接受我在 config.yml 中写的内容:

InvalidArgumentException in YamlFileLoader.php line 399: There is no extension able to load the configuration for "mybundle_tools" (in /home/jeff/Projets/MyProject/src/MyProject/MyBundle/DependencyInjection/../Resources/config/config.yml). Looked for namespace "mybundle_tools", found none

我好几天都没有让这个自定义配置工作,我认为我的 Configuration.php 和我的 Extension.php 文件中也可能存在一些错误。

你能帮我找出我做错的地方吗?

感谢您的提前! :)

这是我的代码:

services.yml

services:
    mybundle.weather:
        class: MyProject\MyBundle\Service\Weather
        arguments:  ['%mybundle_tools%']

config.yml

mybundle_tools:
    cachedir: '%kernel.cache_dir%/mybundle'
    weather:
        cachefile: '%kernel.cache_dir%/mybundle/weather.json'
        apikey: 'myapikey'
        apiurlbase: 'http://api.openweathermap.org/data/2.5/weather?q='

Configuration.php

<?php
namespace MyProject\MyBundle\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('mybundle_tools');

            $rootNode
            ->children()
                ->scalarNode('cachedir')->end()
                ->arrayNode('weather')
                    ->children()
                        ->scalarNode('cachefile')->end()
                        ->scalarNode('apikey')->end()
                        ->scalarNode('apiurlbase')->end()
                    ->end()
                ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

MyProjectMyExtension.php

<?php
namespace MyProject\MyBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class MyProjectMyExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        var_dump($configs);
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('config.yml');

        $configuration = new Configuration();

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

        $container->setParameter('mybundle_tools.cachedir', $config['cachedir']);
        $container->setParameter('mybundle_tools.weather.cachefile', $config['weather']['cachefile']);
        $container->setParameter('mybundle_tools.weather.apikey', $config['weather']['apikey']);
        $container->setParameter('mybundle_tools.weather.apiurlbase', $config['weather']['apiurlbase']);
    }
}

【问题讨论】:

    标签: php symfony dependency-injection symfony-2.8


    【解决方案1】:

    您已经在 load 方法中配置了您的包。如果你像这样使用processConfiguration,你会得到你的包配置文件。

    请记住,加载方法仅包含您的捆绑配置。如果要访问完整配置,则需要实现prepend 接口。

    public function load(array $configs, ContainerBuilder $container)
    { 
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
    
        ....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 2017-05-05
      • 1970-01-01
      • 2015-02-16
      • 2019-11-20
      相关资源
      最近更新 更多