views:

58

answers:

3

I have a large php project that relies on two levels of config files.

In my project folder I have a default.config.ini that is loaded and then merged with a site specific config file.

At the moment the code is reading an environment variable PROJECT_CONFIG that points to the specific config file. This works fine for each developer working on their own machine. The problem arises when we move the project to the server and want to have three instances of the same project: Dev, Stage, Live.

We now no longer can use the global env var since it needs to be different of each subdomain (project is setup as dev.domain.com, stage.domain.com and www.domain.com).

I have considered converting the server variable HTTP_HOST into an env var name and using that to set the right config (i.e. when a user requested a page from dev.domain.com, the code would look for an env var called dev_domain_com), but I wanted to see what other people are doing and what they recommend.

Any advice would be greatly appreciated, thanks in advance

+1  A: 

Use apache's SetEnv directive to set your PROJECT_CONFIG in the container configuring access to the application instance:

SetEnv PROJECT_CONFIG /src/dev/app.config.php
David Schmitt
It should be noted that you can retrieve this value with: `$_ENV['PROJECT_CONFIG']` or `getenv('PROJECT_CONFIG')`And also, that David's suggested code should be placed in the .htaccess file for the site's root directory or in the VirtualHost config directives for each "virtual host" in the Apache config.
sholsinger
A: 

I think your current solution is just fine. The environment settings are there for a reason after all. I've seen your way of doing things used for a large scale proprietary cms I used to work on.

I need to do the same thing with having dev/stage/production sites that load different configurations and plan to use environment settings and have it detect the hostname as a fallback if the env setting isn't set.

spatel
+1  A: 

David Schmitt's idea is the most elegant. I could see a case for using $_SERVER['HTTP_HOST'] to determine which server you're on and set the path accordingly. One such case could be if you do not have privileges to modify the server's virtual host configuration files. For instance:

<?php
  switch($_SERVER['HTTP_HOST']){
    case 'dev.domain.com':
      $path = '/home/dev_user/project.config.ini';
      break;
    case 'stage.domain.com':
      $path = '/home/stage_user/project.config.ini';
      break;
    case 'www.domain.com':
      $path = '/home/live_user/project.config.ini';
      break;
    default:
      $path = '';
  }
  if(!defined('PROJECT_CONFIG')){
    define('PROJECT_CONFIG', $path);
  }
  unset($path);
?>

Note: Depending on your server configuration you may want to replace $_SERVER['HTTP_HOST'] with $_SERVER['SERVER_NAME'].

sholsinger