如何正确设置Symfony2中的默认配置值?

时间:2022-10-22 17:03:55

I've built a service which I'd like to be able to configure from a config file. I've been able to get it working as needed, but when I look at other bundles, don't see the same config setup as I've had to use. I feel like it's a hack. What I need is to have a configuration value as optional, meaning if it isn't in the config.yml file it uses a default value. I've accomplished this by adding the following to my Configuration.php bundle file:

我已经构建了一个服务,我希望能够从配置文件中进行配置。我已经能够根据需要使其工作,但是当我查看其他捆绑包时,看不到我必须使用的相同配置设置。我觉得这是一个黑客。我需要的是将配置值设置为可选,这意味着如果它不在config.yml文件中,则使用默认值。我通过在Configuration.php包文件中添加以下内容来完成此操作:

namespace ChrisJohnson00\ApiProfilerBundle\DependencyInjection;

...
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('chris_johnson00_api_profiler');

    $rootNode
        ->children()
            ->scalarNode('warning_threshold')
                ->info('Changes the warning threshold time (ms).  This is used to change the toolbar to yellow when the total response time is > this value')
                ->example("5000")
                ->defaultValue("5000")
                ->end()
            ->scalarNode('error_threshold')
                ->info('Changes the error threshold time (ms).  This is used to change the toolbar to red when the total response time is > this value')
                ->example("10000")
                ->defaultValue("10000")
                ->end()
        ->end()
    ;

    return $treeBuilder;
    }
}

But this didn't work alone, I had to add the following to my bundle extension file's load function

但这不能单独工作,我不得不将以下内容添加到我的bundle扩展文件的load函数中

namespace ChrisJohnson00\ApiProfilerBundle\DependencyInjection;

...
class ChrisJohnson00ApiProfilerExtension extends Extension
{

    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config        = $this->processConfiguration($configuration, $configs);

        $container->setParameter('chris_johnson00_api_profiler.warning_threshold', $config['warning_threshold']);
        $container->setParameter('chris_johnson00_api_profiler.error_threshold', $config['error_threshold']);

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

How can I configure my bundle's configuration parameters without the need for $container->setParameter(...) in my extension file?

如何在我的扩展文件中配置我的bundle的配置参数而不需要$ container-> setParameter(...)?

1 个解决方案

#1


0  

Found a cookbook with the answers... I'm doing it correctly it seems. http://symfony.com/doc/current/cookbook/bundles/extension.html

找到了一本带有答案的食谱......我似乎正确地做到了。 http://symfony.com/doc/current/cookbook/bundles/extension.html

#1


0  

Found a cookbook with the answers... I'm doing it correctly it seems. http://symfony.com/doc/current/cookbook/bundles/extension.html

找到了一本带有答案的食谱......我似乎正确地做到了。 http://symfony.com/doc/current/cookbook/bundles/extension.html