Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

symfony

Symfony holds configuration parameters in /app/config/parameters.ini.

I'd like to to use different sets of parameters based on the hostname. Initially the hostname will determine the database to use but may well expand to cover more.

I'd prefer that the parameters per host are stored in a separate file so as to make this easy to programmatically generate.

Conceptually I'd like to store configuration parameters like this:

/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 is referenced in \Sensio\Bundle\DistributionBundle\Controller\ConfigurationController and that modifying this file should work.

Is this be best approach? Is there a more straightforward approach that does not require patching the core framework?

like image 950
Jon Cram Avatar asked Dec 27 '22 05:12

Jon Cram


2 Answers

I would think about different environments which use different parts for one ini file. You could have a prod1 environment using parameters prefixed with prod1 and a 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 stuff you want to read from the parameters.ini:

config_prod1.yml:

imports:
    - { resource: config_prod.yml }

// .. overwrite stuff here

This way you also get around the caching issue, as you already have on cache per environment.

To seperate the two, either create and use a app_prod1.php and app_prod2.php as you would with the dev environment or change the environment depending on the host in you app.yml.

like image 100
Sgoettschkes Avatar answered Jan 05 '23 09:01

Sgoettschkes


If your parameters depend on the host, I guess you'd prefer to manage them from the web server and not from Symfony project config that must be shared between all instances...

Here is the way to define some parameters in the 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 a YAML configuration file :

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

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

like image 44
AlterPHP Avatar answered Jan 05 '23 10:01

AlterPHP