php - Symfony 配置默认值

标签 php configuration symfony

我相信我的配置是正确的,但我想要我的 redis 端口和方案配置选项的默认值,但它们显示为空值?

谁能看出是什么问题?

enter image description here

这是我的配置。

/**
 * {@inheritdoc}
 */
public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();

    $rootNode = $treeBuilder->root('company_name');
    $rootNode
        ->children()
            ->arrayNode('cache')
                ->children()
                    ->arrayNode('redis')
                        ->addDefaultsIfNotSet()
                        ->treatNullLike([
                            'scheme' => 'tcp',
                            'port' => 6379,
                        ])
                        ->children()
                            ->scalarNode('scheme')
                                ->defaultValue('tcp')
                            ->end()
                            ->scalarNode('host')
                                ->isRequired()
                                ->cannotBeEmpty()
                            ->end()
                            ->integerNode('port')
                                ->defaultValue(6379)
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ->end();

    return $treeBuilder;
}

这是我的 parameters.yml 文件

parameters:
    company_name:
        cache:
            redis:
                host: dev-sessionstore.companyname.com
                schema: ~
                port: ~

控制台输出:

$ php bin/console config:dump-reference CompanyNameCacheBundle
# Default configuration for "CompanyNameCacheBundle"
company_name:
    cache:
        redis:
            namespace:            apps
            scheme:               tcp
            host:                 ~ # Required
            port:                 6379
        apcu:
            namespace:            phpcache

我希望方案和端口使用默认值,但什么导致它们为空?

最佳答案

我知道这是一个老问题,但我在谷歌搜索另一个问题时偶然发现了它,发现它没有答案。

问题是您只指定了如何处理整个 redis 数组的 null,而不是 schemeport 值。您指定了它们的默认值,但是因为您将这些单独的键设置为 null,所以您需要指定如何为每个键处理 null:

/**
 * {@inheritdoc}
 */
public function getConfigTreeBuilder()
{
    // Using an array so the values only need to be changed in one place
    $redisDefaults = [
        'scheme' => 'tcp',
        'port' => 6379,
    ];

    $treeBuilder = new TreeBuilder();

    $rootNode = $treeBuilder->root('company_name');
    $rootNode
        ->children()
            ->arrayNode('cache')
                ->children()
                    ->arrayNode('redis')
                        ->addDefaultsIfNotSet()
                        ->treatNullLike($redisDefaults)
                        ->children()
                            ->scalarNode('scheme')
                                ->defaultValue($redisDefaults['scheme'])
                                ->treatNullLike($redisDefaults['scheme'])
                                // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                            ->end()
                            ->scalarNode('host')
                                ->isRequired()
                                ->cannotBeEmpty()
                            ->end()
                            ->integerNode('port')
                                ->defaultValue($redisDefaults['port'])
                                ->treatNullLike($redisDefaults['port'])
                                // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ->end();

    return $treeBuilder;
}

你的参数文件也有错别字,应该是scheme,而不是schema

关于php - Symfony 配置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35268403/

相关文章:

php - 回应查询不起作用(返回 "Resource id #18")

android - android应用程序中的proguard

hadoop - 如果 hadoop 中的节点更改其 IP 地址会发生什么?

php - Doctrine 选择多个对象

mysql - Doctrine2和where sql语句

PHP - 使图像背景透明并具有容差

php - 如何将数组中的字符串分解为更多数组 PHP

java - 如何使用 IntelliJ 在特定于环境的运行时配置之间轻松切换?

php - 我如何在 symfony3 中使用 "INSERT SELECT"?

php - 进行 AJAX 调用以更新数据库 onChange of a textarea