Jordi Jordi - 2 months ago 6
PHP Question

Symfony service with different value params by environment

I would like to set in a service different param values by environment with Symfony. The following doesn't work, but it's to give an idea...

config.yml

mytest:
key: "VALUE_1"


config_dev.yml

mytest:
key: "VALUE_2"


services.yml

apiclient:
class: My\Class
arguments:
apikey: "@mytest.key"

Answer

The easiest way for your case it's define just a parameter:

In config.yml under section parameters add

parameters:
    mytest.key: "VALUE_1"

Similarly in config_dev.yml (if there's no parameters section, just create it)

parameters:
    mytest.key: "VALUE_2"

Then you make it:

apiclient:
    class:        My\Class            
    arguments:
        apikey: "%mytest.key%"

I don't think it's possible to make nested parameters. And making config values is possible, but would be clear case of over-engineering in this case.

Comments