简体   繁体   中英

Symfony2 Composer and environment variables

I would like to set the configuration of my symfony2 project using environment variables.

In the server I have defined:

SYMFONY__DATABASE__USER
SYMFONY__DATABASE__PASSWORD
SYMFONY__DATABASE__NAME
SYMFONY__DATABASE__HOST
SYMFONY__DATABASE__DRIVER

My parameters.yml.dist looks like this:

#app/config/parameters.yml.dist
parameters:
    database_host:     "%database.host%"
    database_port:     ~
    database_name:     "%database.name%"
    database_user:     "%database.user%"
    database_password: "%database.password%"
    database_driver:   "%database.driver%"

when I run composer I get an exception

composer install --dev --no-interaction --prefer-source
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]                                                   
You have requested a non-existent parameter "database.driver". Did you mean one of these: "database_user", "database_driver"?

These variables are defined in the server so I can modify the parameters.yml.dist to define these values. But this does not seams the right way, because wat I really want to use are the environment variables.

Note: I want to read this environment variables in travis, heroku and my vagrant machine. I only want to have in the repository the vagrant machine variables.

Which is the proper way to do this? How should look my parameters.yml.dist?

Looks you are doing everything okay.

Here is the complete documentation for Setting Environment Variables which I believe you already read.

What is important to note is this:

Also, in order for your console to work (which does not use Apache), you must export these as shell variables. On a Unix system, you can run the following:

$ export SYMFONY__DATABASE__USER=user
$ export SYMFONY__DATABASE__PASSWORD=secret

I remember once I have a similar issue, I was setting everything on APACHE, but when running commands it wasn't working because I forgot to EXPORT the variables on the system.

Be aware that using export is a temp solution, if you reset your server those values will be lost, you will need to setup in a permanent way according to your OS.

I think you solved this long time ago, but the problem is actually that you have 2 _ between DATABASE and USER and the parser for this have a string replace function that replaces every __ with a . .

For your example to work you should have written like this:

SYMFONY__DATABASE_USER -> database_user SYMFONY__DATABASE__USER -> database.user

You can try this bundle if your system version is >= 2.6.2:

This bundle provides a way to read parameters from environment variables at runtime. The value defined in the container parameter is used as fallback when the environment variable is not available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM