gpt4 book ai didi

php - Symfony2 中的自定义配置

转载 作者:可可西里 更新时间:2023-10-31 23:46:55 26 4
gpt4 key购买 nike

我开发了一个提供标签云 功能的小包。将它包含在其他 Symfony 项目中应该很容易,因此它需要是可配置的。我发现了 3 个页面:

我按照示例进行操作,但很明显,我错过了一些东西,因为当我使用 php app/console config:dump-reference 时收到以下错误消息:

[Symfony\Component\Config\Exception\FileLoaderLoadException] There is no extension able to load the configuration for "loew_tag" (in somePath/blog/app/config/../../src/Loew/TagBundle/Resources/config/config.yml). Looked for namespace "loew_tag", found " ... " in somePath/blog/app/config/../../src/Loew/TagBundle/Resources/config/config.yml (which is being imported from "somePath/blog/app/config/config.yml").

[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
There is no extension able to load the configuration for "loew_tag" (in /home/somePath/blog/app/config/../../src/Loew/TagBundle/Resources/config/config.yml). Looked for namespace "loew_tag", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "blog", "fos_user", "debug", "web_profiler", "sensio_distribution"

我在“博客包”中工作并尝试访问“标签包”的配置数据。

我的“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');
}
}

配置文件:

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

配置.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 的条目在所有文件中都有注释,以使其尽可能简单。

我注意到,已经提出了类似的问题并且相关问题已经解决,但我无法将解决方案转移到这个问题。

知道吗,我错过了什么?

最佳答案

终于解决了这个问题

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

显然,配置文件将在服务加载后立即自动加载。

关于php - Symfony2 中的自定义配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31991695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com