[Tip of the Week] Using Environment Control in ColdBox

One of the most common server configurations is to have a production server and then 1 or more development or testing servers. The trick with your “lower” environments is you typically want different settings for logging, error messages, data sources, or outgoing E-mails. Manually switching settings when you move code is sketchy at best and setting up deployment scripts can be more work than you’re willing to take on.

Enter ColdBox Environment Control. ColdBox makes it easy to have different settings for each environment. In your configuration CFC, you have a configure() method that creates several structs of setting variables. Let’s consider these our default production values. Next, all you do is create a method for each additional environment such as development(), stage(), etc. ColdBox will automatically call the appropriate environment override and you can add, remove, or override settings for that environment as you see fit.

In the mock example below, you can see that the main production settings are in the configure() method. The “environments” setting struct declares a list of regular expressions to match against the URL to determine the environment. When not in production, the appropriate development() or stage() method will be called where it can override or add settings as it sees fit.

ColdBox.cfc
component {

function configure() {
coldbox = {
setting1 = ‘value1’,
setting2 = ‘value2’,
setting3 = ‘value3’
};

environments = {
development = “^dev.,^local.”,
stage = “^stage.,^test.”
};
}

function development() {
coldbox.setting1 = ‘devValue’;
arrayAppend(interceptors, {class="coldbox.system.interceptors.ColdboxSidebar} );
}

function stage() {
coldbox.setting1 = ‘stageValue’;
}
}

More info here: http://wiki.coldbox.org/wiki/ConfigurationCFC.cfm#environments

P.S. Don’t want to use URL to determine your environment? No problem. Instead of an environments struct in your config, create a method called detectEnvironment() and simply have it return a string corresponding with the correct environment for that server. You can base off the machine name, IP address, or even the location of the code on the file system. It’s up to you!

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com