[英]Symfony : get configuration values in a bundle
I create a bundle with a configuration class : 我用配置类创建一个包:
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mongo_image');
$rootNode
->children()
->scalarNode('mongo_server')
->cannotBeEmpty()
->isRequired()
->info('Mongo server')
->end();
$rootNode->children()
->scalarNode('mongo_port')
->defaultValue(27017)
->info('Mongo port')
->end();
........
return $treeBuilder;
}
}
then I set the values in my config.yml. 然后在config.yml中设置值。
How to get this values in a controller ? 如何在控制器中获取此值?
add YourExtension class 添加YourExtension类
namespace YourBundle\DependencyInjection;
use ...
class YourExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
foreach ($config as $parameter => $name) {
$container->setParameter(
'mongo_image.'.$parameter,
$name
);
}
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.