Stephen Senkomago Musoke
Stephen Senkomago Musoke

Reputation: 3523

Referencing Nested Parameters in Symfony 4 Configuration Files

I would like to be able to use nested parameters defined in one configuration file in another for examle

# config/config.yaml (my custom configuration file)
database:
    driver: pdo_mysql
    host: localhost
    user: dev
    password: dev
    dbname: dev

To be used in another configuration file

# config/packages/doctrine.yaml
url: 'mysql://%database.user%:%database.password%@%database.host%:%database.port%/%database.dbname%'

This currently does not work as I am not sure how to reference the variables in the Config package (https://symfony.com/doc/current/configuration.html)

Upvotes: 3

Views: 1160

Answers (1)

kcm
kcm

Reputation: 1348

Parameters are just flat key-value elements and can not be nested. This is written in the Symfony Documentation

You have to define a separate key for every parameter. The convention is to use a dot to separate namespaces. The dot does not make it an array, but makes it easier to read.

database.driver: pdo_mysql
database.host: localhost
database.user: dev
database.password: dev
database.dbname: dev

The parameters are then accessed exactly like you want it:

url: 'mysql://%database.user%:%database.password%@%database.host%:%database.port%/%database.dbname%'

Upvotes: 1

Related Questions