Symfony2中的自定义配置

4
我开发了一个小包,提供标签云功能。它应该很容易地被包含在其他Symfony项目中,因此需要可配置性。我发现了三个页面: 我沿着这些示例工作,但显然我缺少一些东西,因为当我使用php app/console config:dump-reference时,我会收到以下错误消息:

[Symfony\Component\Config\Exception\FileLoaderLoadException] 没有扩展能够加载“loew_tag”(在somePath/blog/app/config/../../src/Loew/TagBundle/Resources/config/config.yml中)。查找命名空间“loew_tag”,在somePath/blog/app/config/../../src/Loew/TagBundle/Resources/config/config.yml中找到“…”(正在从“somePath/blog/app/config/config.yml”导入)。

[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
没有扩展能够加载“loew_tag”(在/home/somePath/blog/app/config/../../src/Loew/TagBundle/Resources/config/config.yml中)。查找命名空间“loew_tag”,在“framework”、“security”、“twig”、“monolog”、“swiftmailer”、“assetic”、“doctrine”、“sensio_framework_extra”、“blog”、“fos_user”、“debug”、“web_profiler”、“sensio_distribution”中找到。

我在“blog bundle”内工作,尝试访问“tag bundle”的配置数据。
我的“app/config/config.yml”顶部:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: ../../src/Loew/TagBundle/Resources/config/services.yml }
- { resource: ../../src/Loew/TagBundle/Resources/config/config.yml }

LoewTagExtension.php:

<?php
// Loew/TagBundle/DependencyInjection/LoewTagExtension.php

namespace Loew\TagBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class LoewTagExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        //$container->setParameter('food_entities',     $config['food_entities']);
        $container->setParameter('split_match', $config['split_match']);

        $loader = new Loader\YamlFileLoader($container, new   FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('config.yml');
        $loader->load('services.yml');
        }
    }

config.yml:

loew_tag:
#    food_entities:
#     - "BlogBundle:Article"
#     - "BlogBundle:Comment"
    split_match: "/[^0-9a-zA-ZöÖüÜäÄß]/"

Configuration.php:

<?php
// Loew/TagBundle/DependencyInjection/Configuration.php


namespace Loew\TagBundle\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();

        $rootNode = $treeBuilder->root('loew_tag');

        $rootNode
            ->children()
            ->scalarNode('split_match')->end()
//                ->arrayNode('food_entities')
//                ->prototype('scalar')->end()

            ->end();

        return $treeBuilder;
    }
}

为了尽可能保持简洁,所有文件中有关节点food_entities的条目都被注释掉了。

我注意到,类似的问题已经被问过并且相关的问题已经得到解决,但是我无法将这些解决方案应用到这个问题上。

您有什么想法吗?我错过了什么吗?


1
你的 app.php 中是否注册了该 bundle?另外,执行 php app/console config:dump-reference 命令会输出什么内容? - ferdynator
谢谢您的回复。假设您所说的文件名为/app/AppKernel.php:是的,该bundle在那里注册了: new Loew\TagBundle\TagBundle(), 控制台命令导致上述错误消息。 - dan
2
你的 DI 扩展加载了吗?我看到你的 bundle 名称和扩展名称之间存在不匹配,LeowTagBundle 与 TagBundle。 - Jakub Zalas
Jakub,我按照http://symfony.com/doc/current/cookbook/bundles/extension.html中的示例进行操作,如下所示: 像src\VendorName\BundleNamesrc\Acme\HelloBundlesrc\Loew\TagBundle这样的结构需要将扩展类的命名为AcmeHelloExtensionLoewTagExtension。 难道我理解错了,供应商名称不应该是扩展类名的一部分吗?无论如何,感谢您的提示! - dan
1
是的,但供应商也应该是捆绑类名称的一部分。请参阅http://symfony.com/doc/current/cookbook/bundles/best_practices.html#bundle-name - Jakub Zalas
显示剩余3条评论
1个回答

0

最终通过以下方式解决了问题:

  • 谨记命名约定,特别是 Jakub Zalas 指出的部分
  • 从扩展文件中删除条目:$loader->load('config.yml');

显然,一旦服务已经加载,配置文件将自动加载。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接