我已经构建了一个服务,我希望能够从配置文件进行配置。 我已经能够根据需要让它工作,但是当我查看其他捆绑包时,看不到与我必须使用的相同的配置设置。 我觉得这是一个黑客。我需要的是一个可选的配置值,这意味着如果它不在 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