Autoloading Development or Production configs (best practices)
- by Xeoncross
When programming sites you usually have one set of config files for the development environment and another set for the production server (or one file with both settings). I am assuming all projects should be handled by version control like git or svn. Manual file transfers (like FTP) is wrong on so many levels.
How you enable/disable the correct settings (so that your system knows which ones to use) is a problem for me. Each system I work on just kind of jimmy-rigs a solution. Below are the 3 methods I know of and I am hoping that someone can submit a more elegant solutions.
1) File Based
The system loads a folder structure based on the URL requested.
/site.com
/site.fakeTLD
/lib
index.php
For example, if the url is http://site.com then the system loads the production config files located in the site.com folder. However, if I'm working on the site locally I visit http://site.fakeTLD to work on the local copy of the site.
To setup this I edit my hosts
file and add site.fakeTLD to point to my
own computer (127.0.0.1/localhost) and then create a vhost
in apache.
So now I can work on the codebase locally and then push to the server without any trouble. The problem is that this is susceptible to a "host" injection attack. So someone loading site.com could set the host to site.fakeTLD and then the system would load my development config files instead of production.
2) Config Based
The config files contain on section for development - and one for production. The problem is that each time you go to push your changes to the repo you have to edit the file to specify which set of config options should be used.
$use = 'production'; //'development';
This leaves the repo open to human error should one of the developers forget to enable the right setting.
3) File System Check Based
All the development machines have an extra empty file called "development.txt" or something. Each time the system loads it checks for this file - if found then it knows it is in development mode - if missing then it knows it is in production mode. Since the file is NEVER ADDED to the repo then it will never be pushed (and checked out) on the production machine.
However, this just doesn't feel right and causes a slight slow down since all filesystem checks are slow.
Is there anyway that the server can auto-detect wither to use the development or production configs?