user396070
user396070

Reputation:

What's an easy way to manage local/development/production configuration files in PHP projects?

I would like to have seperate copies of configuration files for local/dev/production, but not have them interfere with each other and possibly be able to co-exist in version control.

What is an elegant solution to this problem?

Upvotes: 4

Views: 1667

Answers (1)

user396070
user396070

Reputation:

Try this:

<?php 

// rename this file to whatever your configuration file is supposed to be called (wp-config.php, etc.).

switch ($_SERVER['SERVER_NAME']):

// assuming your local server name is 'localhost,' leave this section alone
    case 'localhost':
        require_once('local.config.php');
        break;

// replace production-url with the server name for your production site
    case 'production-url':
        require_once('production.config.php');
        break;

// replace development-url with the server name for your development site
    case 'development-url':
        require_once('development.config.php');
        break;

    default:
     // something went wrong;

endswitch;
// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!

Source

Upvotes: 2

Related Questions