Symfony2 - Defining a boolean parameter in .ini parameters

I am having problems defining a boolean parameter in the parameters.ini file. This definition:

 aParameter = true 

Then in config.yml I do:

 aParameter: %aParameter% 

But I get this error:

InvalidTypeException: Invalid type for path "myService.aParameter". The expected value is boolean, but received a string.

This error disappears when I replace %aParameter% with true . What am I doing wrong?

+6
source share
3 answers

There seems to be no way to achieve this. See this question . The easiest way is to replace the parameters.ini parameters with .yml parameters

For example, parameters.yml.dist from Symfony-standard

+2
source

Symfony2 default import options are in YAML format, so one of the first lines should be:

 imports: - { resource: parameters.yml } 

And in .yml options use:

 aParameter: true 

I do not use INI files, so I do not know how this works.

+3
source

Symfony 2 uses the parse_ini_file () function and does not seem to return the correct type for a boolean.

 <?php file_put_contents('test.ini', "test1=on\ntest2=true\ntest3=1"); var_dump(parse_ini_file('test.ini')); 

displays

 array(3) { 'test1' => string(1) "1" 'test2' => string(1) "1" 'test3' => string(1) "1" } 

You might want to implement your own ini analyzer by taking Symfony\Component\DependencyInjection\Loader\IniFileLoader as an example.

An example of lexer that supports booleans can be found in the comments in the php doc for parse_ini_file ()

An alternative would be to use the yaml format for your configuration file.

+3
source

All Articles