tleb
tleb

Reputation: 4616

Manage two databases

I have a config file (.php) in my project, which contains an array with the settings for the all project. This array has a structure similar to this:

'db' =>
    'prod' =>
        'host'     => ''
        'dbname'   => ''
        'username' => ''
        'password' => ''
    'local' =>
        'host'     => ''
        'dbname'   => ''
        'username' => ''
        'password' => ''
'other' => 'settings'

My question is: how will I manage those double connection information ?

Should I, later in this file, if I am in local, put the local information in the prod array ? Or should I use multiple files for configuration, and include the good one in this config.php (which I would then include in every file of this project) ?

How would you do it ?

Thank you.

Upvotes: 0

Views: 57

Answers (2)

tleb
tleb

Reputation: 4616

By using the comment of @Dagon and the answer of @Pere Pages, I arrived to a solution which suits me:

In my $config array, I have 3 arrays: use, prod and local.

In use I can put settings which won't change depending on the environment (production or localhost) and I also have the settings which change depending on the environment, but they are empty.

In prod and local, I have the settings adapted for each environment (the ones which are empty in use).

In the same file, I go over each line of use (foreach), and for the empty ones, I fill them with the appropriate value coming from prod or local, depending on my APPLICATION_ENV (is set to local on my machine).

Thank you.

Upvotes: 0

pearpages
pearpages

Reputation: 22017

As @Dagon says, usually you detect in which enviroment you are and read one configuration or another.

You can detect it through PHP. You can read the Hostname from PHP and define the constant accordingly:

if(!defined('APPLICATION_ENV')) {
   if(FALSE === stripos($_SERVER['SERVER_NAME']), 'www.example.com') {
       define(APPLICATION_ENV, 'development');
   } else {
       define(APPLICATION_ENV, 'production');
   }
}

This way, you don't have to rely on the environment setting at all.

Once this has been set (either in your Apache's configuration, or at the system level), you can read its value using the getenv function :

echo getenv('APPLICATION_ENV');

If you want to know more about this you can read this post: Set Application_ENV via virtual host config and read this in PHP

Upvotes: 1

Related Questions