Symfony2捆绑配置问题

时间:2014-01-03 13:57:27

标签: php symfony configuration

我正在尝试公开为我的bundle定义的自定义配置,称为DecisionBundle。

我有一个Decision \ DecisionBundle \ Resources \ config \ custom.yaml,如下所示:

decision:
    testValue: 11
    anotherTest: 12

我还有一个看起来像这样的扩展名:

namespace Decision\DecisionBundle\DependencyInjection;

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

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

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

文件custom.yml似乎已加载好了。接下来是配置界面

namespace Decision\DecisionBundle\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('decision');

        $rootNode
            ->children()
                ->scalarNode('testValue')->defaultValue('0')->end()
                ->scalarNode('anotherTest')->defaultValue('0')->end()
            ->end();

        // Here you should define the parameters that are allowed to
        // configure your bundle. See the documentation linked above for
        // more information on that topic.

        return $treeBuilder;
    }
}

然而,这导致以下结果:

InvalidArgumentException: There is no extension able to load the configuration for "decision" (in *****\src\Decision\DecisionBundle\DependencyInjection/../Resources/config\custom.yml). Looked for namespace "decision", found none

有人能指出我做错了什么吗?我需要以某种方式手动注册扩展吗?但是,扩展似乎被考虑在内,因为它看起来是从调试信息中加载的。 谢谢!

1 个答案:

答案 0 :(得分:0)

目前,您在custom.yml文件中设置参数的方式与在`app / config / config.yml'文件中设置参数的方式相同。

decision:
    testValue: 11
    anotherTest: 12

当您通过配置文件加载文件时,您的服务文件(在您的情况下为services.yml& custom.yml)应该有servicesparameters作为头。要在custom.yml文件中使用您的参数,您应该将其列为...

parameters:
    decision.testValue: 11
    decision.anotherTest: 12

另外,如果您要将参数放在app/config/config.yml文件中,则需要将它们设置为DecisionExtension中的实际参数。为此,您需要从Configuration文件中获取数据并将其设置在容器中,如...

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

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

$container->setParameter('decision.testValue', $config['testValue']);
$container->setParameter('decision.anotherTest', $config['anotherTest']);

然而,这会覆盖custom.yml中设置的参数,并且还会否定它存在的任何原因。

相关问题