Default Values ​​for Symfony

I believe my configuration is correct, but I need the default values ​​for the redis port parameter and the circuit configuration, but do they exit as zeros?

Can anyone understand what the problem is?

enter image description here

Here is my configuration.

/**
 * {@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;
}

And here is my parameters.yml file

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

Console output:

$ 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

I want the circuit and port to use the default values, but why were they empty?

+4
source share
1 answer

I know this is an old question, but I stumbled upon it, and Googling - on another problem and saw that it did not answer.

, , null redis, scheme port. , 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

+1

Source: https://habr.com/ru/post/1627778/


All Articles