[coldbox-4]: How to detect environment outside config

Folks,

I was wondering if ColdBox has a built-in variable or method to determine which environment (development, staging, or production) the site is currently running. I would like to call the variable/method in controllers.

Thanks!

This should be what you are looking for I think.

Hi Tim,

Thanks for the reply.
But that specific method is only applied in ColdBox configuration. I’m looking for the one that I can call from controller.

From a controller (or view), you would call #getSetting("environment")# to retrieve the current environment setting.

Jon

What you could do, as I can’t recall if the variable is exposed or if one is even available. Is to setup a variable yourself.

Then use the getSettings to read back and detect if it is production or not based on that variable.

In the past I used an interceptor to do this, to globally set a variable for my application.

Thanks everyone!

In config/ColdBox.cfc - configure(), I defined settings.environment = “production”; AND in development(), I defined settings.environment = “development”;
From controller or view, I can call getSetting(“environment”) to determine what environment I am now.

It worked!

No, you don’t need to manually set the “environment” setting, that’s handled for you! It defaults to production, and if one of your environment regex’s are matched, then it automatically gets set to that environment name. All you need to do is use it.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Hi Brad,

I knew that. But, my question was “how to determine environment outside the configuration file, such as from controllers or views.”
I tested out. It turned out I don’t need to define environment variable. I just call getSetting(“environment”); from my controllers and magically it tells me in which environment my app is running. Beautiful…:slight_smile: