加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Symfony2中的自定义配置

发布时间:2020-12-13 22:23:26 所属栏目:PHP教程 来源:网络整理
导读:我开发了一个小包,提供标签云功能.将其包含在其他Symfony项目中应该很容易,因此需要进行配置.我发现3页: How to Create Friendly Configuration for a Bundle Defining and Processing Configuration Values How to Load Service Configuration inside a Bu
我开发了一个小包,提供标签云功能.将其包含在其他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:

[SymfonyComponentConfigExceptionFileLoaderLoadException]
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”).

[SymfonyComponentDependencyInjectionExceptionInvalidArgumentException]
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 LoewTagBundleDependencyInjection;

use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentConfigFileLocator;
use SymfonyComponentHttpKernelDependencyInjectionExtension;
use SymfonyComponentDependencyInjectionLoader;

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 LoaderYamlFileLoader($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 LoewTagBundleDependencyInjection;

use SymfonyComponentConfigDefinitionBuilderTreeBuilder;
use SymfonyComponentConfigDefinitionConfigurationInterface;

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’);来自扩展文件.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读