Symfony:获取捆绑包中的配置值

时间:2015-09-07 09:28:52

标签: symfony configuration bundle

我创建了一个包含配置类的包:

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中设置了值。

如何在控制器中获取此值?

1 个答案:

答案 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');
}