我创建了一个包含配置类的包:
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;
}
}
然后我在config.yml中设置了值。
如何在控制器中获取此值?
答案 0 :(得分:0)
添加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');
}