Productivity tip #2 - multi-environments config file
Use "smart" config files.
Did you ever had the problem of overwriting by accident your configuration file (whether it is web.config or config.php or any other) on a server (staging/live), and suddenly realising… Ooooops, I thinik I've just broken the system…?
If not - congratulations to you, you can stop reading
But if you still have this kind problems - here comes the solution.
It is - a smart config file.
Implementation is quite simple:
- Find some environment variable that differentiates your environments
- Make note of the values of that variable on your environments
- Define all the possible values of your connection strings etc in one file divided for sections for each environment
- Alter the procedure that reads these variables to take the current environment into account
- And last but not least - also define the LIVE section as a "default" one. This is crucial for systems that might experience some configuration changes (and most of systems are subject to that)
Sample PHP code:
<?php
$config["mycomputer"]["connectionstring"] = "//value local";
$config["www.mywebsite.com"]["connectionstring"] = "//value live";
$config["mytestserver"]["connectionstring"] = "//value test";
?>
In Asp.Net for example you can use Custom Configuration Sections, you'll find the appropriate classes in MSDN.
But what exactly does this give you?
The peace of mind, that you can always transfer all the files to another host and you won't break the whole site/system.