Symfony2: How can I use different parameters (.ini) for the host name?

Symfony contains configuration options in /app/config/parameters.ini .

I would like to use different sets of parameters based on the host name. Initially, the host name will determine the database to be used, but can expand to cover more.

I would prefer that the parameters on the host are stored in a separate file to make it easily programmatically generated.

Conceptually, I would like to save the configuration settings as follows:

 /app/config/parameters.ini /app/config/foo.example.com.parameters.ini /app/config/bar.example.com.parameters.ini 

I see that /app/config/parameters.ini refers to \Sensio\Bundle\DistributionBundle\Controller\ConfigurationController and that modifying this file should work.

Is this a better approach? Is there a simpler approach that does not require correction of the basic structure?

+4
source share
2 answers

I would think of different environments that use different parts for the same ini file. You may have a prod1 environment using parameters prefixed with prod1 and prod2 with the same:

parameters.ini:

 [parameters] prod1_database_driver = pdo_mysql prod1_database_host = 127.0.0.1 # ... prod2_database_driver = pdo_mysql prod2_database_host = localhost 

They both use the prod.yml configuration, but overwrite the material you want to read from the .ini options:

config_prod1.yml:

 imports: - { resource: config_prod.yml } // .. overwrite stuff here 

In this way, you will also get around the caching problem, since you already have a cache for the environment.

To separate them, create and use app_prod1.php and app_prod2.php, as in the dev environment, or change the environment depending on the host in your application.

+5
source

If your parameters are host-dependent, I think you would prefer to manage them from a web server, rather than from a Symfony project configuration, which should be shared between all instances ...

Here is a way to define some parameters in Apache configuration:

 <VirtualHost *:80> ServerName Symfony2 DocumentRoot "/path/to/symfony_2_app/web" DirectoryIndex index.php index.html SetEnv SYMFONY__DATABASE__HOST 192.168.10.10 SetEnv SYMFONY__DATABASE__USER user SetEnv SYMFONY__DATABASE__PASSWORD secret <Directory "/path/to/symfony_2_app/web"> AllowOverride All Allow from All </Directory> </VirtualHost> 

How to access them from the YAML configuration file:

 doctrine: dbal: driver pdo_mysql dbname: symfony2_project host: %database.host% user: %database.user% password: %database.password% 

More information here: http://symfony.com/doc/current/cookbook/configuration/external_parameters.html

+5
source

Source: https://habr.com/ru/post/1399175/


All Articles