6

symfony2の「バンドルのセマンティック構成を公開する方法」の公式マニュアルに従っています。

私は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");

動作しません。

私が間違っているのは何ですか?

4

2 に答える 2

11

次の行w9UserExtension.phpprocessConfiguration追加するだけです

$container->setParameter('w9_user.logintext', $config['logintext']);
于 2012-04-15T17:45:40.717 に答える
0

@acmeのように、すべての構成値をパラメーターに無差別に追加したかったのですが、数十setParameter行を書き込むだけでは十分ではありませんでした。

そこで、クラスsetParametersに追加するメソッドを作成しました。Extension

/**
 * 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);

        // ...
    }
}

警告

機密性の高い構成情報を忘れると公開され、役に立たない構成を公開するとパフォーマンスが低下する可能性があるため、このような動作を広範囲に文書化および/または必要としない場合、これは悪い習慣になる可能性があります。

さらに、コンテナーのパラメーター・バッグに追加する前に、構成変数を広範囲にサニタイズすることを防ぎます。

parameters:これを使用している場合は、何をしているのかわからない限り、セマンティック構成ではなく、おそらく使用する必要があります。

自己責任。

于 2016-06-01T05:49:49.280 に答える