Optional parameter value in Symfony main config.yml

I would like to write something like this in the main application config.yml

:

my_section
  some_value: %local_value%

      

If the parameter local_value

is taken from a file parameters_local.yml

. However, I would like this value to be optional - if the user of the application does not provide a parameter local_value

in theirs parameters_local.yml

, I would like it to be set to null.

If I leave the config.yml

way I indicated it above and try to omit local_value

from parameters_local.yml

, I get the following error message:

[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]  
You have requested a non-existent parameter "local_value".

      

Is there a way to get around this?

+3


source to share


1 answer


Don't use the parameter at all. The developer must decide what he or she wants to define as a parameter and what not.



  • In your configuration, specify this value as optional:

    $treeBuilder->root('my_section')
        ->children()
            ->scalarNode('some_value')->defaultValue('XYZ')->end()
        ->end()
    ;
    
          

  • Let the user decide what works best:

    # config.yml
    my_section: ~
    
    ---
    
    # config.yml
    my_section:
        some_value: ABC
    
    ---
    
    # parameters.yml
    my_section_some_value: 123
    
    # config.yml
    my_section:
        some_value: %my_section_some_value%
    
    ---
    
    # config.yml:
    parameters:
        my_section_some_value: 321
    
    my_section:
        some_value: %my_section_some_value%
    
          

  • Your application / library MUST NOT depend on any of the above solutions.

+2


source







All Articles