[英]How do I set default configuration value in Symfony2 correctly?
我建立了一个服务,希望能够从配置文件中进行配置。 我已经能够根据需要使它工作,但是当我查看其他捆绑软件时,看不到必须使用的相同配置设置。 我觉得这很hack。 我需要的是将配置值作为可选值,这意味着如果它不在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;
}
}
但这并不能单独起作用,我必须将以下内容添加到包扩展文件的加载功能中
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');
}
}
如何在扩展文件中不需要$container->setParameter(...)
情况下配置包的配置参数?
找到一本包含答案的食谱...看来我做得正确。 http://symfony.com/doc/current/cookbook/bundles/extension.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.