2

Is it possible to have a default configuration parameters in Symfony yeml configuration file. In case where the variable parameter is not available. For example. I have database name coming from Apache configuration, but if I run a command I would like to go with a default database, see below. The example is kind of twigi'ish

doctrine:
dbal:
    driver:   pdo_mysql
    host:     "%database_host%"
    port:     "%database_port%"
    dbname:   "%database.name%"|default('database1')
    user:     "%database_user%"
    password: "%database_password%"
2
  • What is the specific scenario why the parameter would not be defined? Commented Dec 8, 2016 at 15:39
  • Apache send SetEnv SYMFONY__DATABASE__NAME mydatabase but when I run symfony command, Apache is not part of the equitation and errors is thrown as there is no database name Commented Dec 8, 2016 at 17:23

3 Answers 3

1

The only way to do that would be to set the parameter to a default value, which I assume would be early in the configuration file. A theoretic example:

parameters:
    foo: bar

imports:
    - { resource: parameters.yml }

If parameters.yml contains a definition for the %foo% parameter, it will overwrite the previous definition. If parameter.yml does not contain a definition for %foo% however, it will use the earlier defined (default) value.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem with this is that if you do define %foo% in parameters.yml, subsequent runs of composer install will remove it (because it's not in parameters.dist.yml).
0

You can put your default configuration in parameters.yml.dist, this way when someone new joins the project and runs:

composer install

or

composer update

He will be prompted to provide his environment specific values and so parameters.yml will be created.

1 Comment

While you can certainly do this, it has no relevance to the question.
0

I figured out a way around this problem by using "database_prefix" as the default database and other databases just add a suffix to that.

doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "database_prefix%database.name%"
        user:     "%database_user%"
        password: "%database_password%"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.