Symfony2 中的自定义配置
Custom config in Symfony2
我开发了一个小包,提供 标签云 功能。将它包含在其他 Symfony 项目中应该很容易,因此它需要是可配置的。我发现了 3 页:
- How to Create Friendly Configuration for a Bundle
- Defining and Processing Configuration Values
- How to Load Service Configuration inside a Bundle
我按照示例进行了操作,但很明显,我错过了一些东西,因为我在使用 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"
我在 '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
的条目在所有文件中都有注释,以使其尽可能简单。
我注意到,有人问过类似的问题,相关问题已经解决,但我无法将解决方案转移到这个问题上。
知道吗,我想念什么?
终于解决了
- 牢记命名约定,尤其是 Jakub Zalas 指出的部分
- 正在从扩展文件中删除条目:
$loader->load('config.yml');
。
显然,配置文件将在服务加载后立即自动加载。
我开发了一个小包,提供 标签云 功能。将它包含在其他 Symfony 项目中应该很容易,因此它需要是可配置的。我发现了 3 页:
- How to Create Friendly Configuration for a Bundle
- Defining and Processing Configuration Values
- How to Load Service Configuration inside a Bundle
我按照示例进行了操作,但很明显,我错过了一些东西,因为我在使用 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"
我在 '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
的条目在所有文件中都有注释,以使其尽可能简单。
我注意到,有人问过类似的问题,相关问题已经解决,但我无法将解决方案转移到这个问题上。
知道吗,我想念什么?
终于解决了
- 牢记命名约定,尤其是 Jakub Zalas 指出的部分
- 正在从扩展文件中删除条目:
$loader->load('config.yml');
。
显然,配置文件将在服务加载后立即自动加载。