Configuration 如何使用symfony2访问控制器中的语义配置?

Configuration 如何使用symfony2访问控制器中的语义配置?,configuration,symfony,Configuration,Symfony,下面是symfony 2的官方手册“如何公开捆绑包的语义配置” 我有我的configuration.php namespace w9\UserBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; class Configuration impl

下面是symfony 2的官方手册“如何公开捆绑包的语义配置”

我有我的configuration.php

namespace w9\UserBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('w9_user');

        $rootNode
            ->children()
                ->scalarNode('logintext')->defaultValue('Zareejstruj się')->end()
            ->end()
        ;        

        return $treeBuilder;
    }
}
和w9UserExtension.php:

namespace w9\UserBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class w9UserExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}
这听起来可能很傻,但我找不到方法,如何在控制器中访问logintext参数

$logintext = $this->container->getParameter("w9_user.logintext");
不起作用


我做错了什么?

w9UserExtension.php
中,在
processConfiguration
行之后添加

$container->setParameter('w9_user.logintext', $config['logintext']);

我想不加选择地将所有我的配置值添加到参数中,比如@acme,编写几十行
setParameter
行还不够懒

因此,我制作了一个
setParameters
方法来添加到
扩展
类中

/**
 * Set all leaf values of the $config array as parameters in the $container.
 *
 * For example, a config such as this for the alias w9_user :
 *
 * w9_user:
 *   logintext: "hello"
 *   cache:
 *      enabled: true
 *   things:
 *      - first
 *      - second
 *
 * would yield the following :
 *
 * getParameter('w9_user.logintext') == "hello"
 * getParameter('w9_user.cache') ---> InvalidArgumentException
 * getParameter('w9_user.cache.enabled') == true
 * getParameter('w9_user.things') == array('first', 'second')
 *
 * It will resolve `%` variables like it normally would.
 * This is simply a convenience method to add the whole array.
 *
 * @param array $config
 * @param ContainerBuilder $container
 * @param string $namespace The parameter prefix, the alias by default.
 *                          Don't use this, it's for recursion.
 */
protected function setParameters(array $config, ContainerBuilder $container,
                                 $namespace = null)
{
    $namespace = (null === $namespace) ? $this->getAlias() : $namespace;

    // Is the config array associative or empty ?
    if (array_keys($config) !== range(0, count($config) - 1)) {
        foreach ($config as $k => $v) {
            $current = $namespace . '.' . $k;
            if (is_array($v)) {
                // Another array, let's use recursion
                $this->setParameters($v, $container, $current);
            } else {
                // It's a leaf, let's add it.
                $container->setParameter($current, $v);
            }
        }
    } else {
        // It is a sequential array, let's consider it as a leaf.
        $container->setParameter($namespace, $config);
    }
}
然后您可以这样使用:

class w9UserExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        // Add them ALL as container parameters
        $this->setParameters($config, $container);

        // ...
    }
}
警告

如果您不广泛记录和/或不需要这样的行为,这可能是一种不好的做法,因为如果您忘记了敏感的配置信息,您可能会暴露这些信息,并且由于暴露无用的配置而失去性能

此外,它还防止您在将配置变量添加到容器的参数包之前对其进行全面清理

如果您正在使用它,您可能应该使用
参数:
而不是语义配置,除非您知道自己在做什么


使用风险自负。

如果我有一堆配置参数,我如何一次设置它们呢?@acme我也想这么做。见下面我的答案。