5.7 - Detect Current Environment

Permalink 1 user found helpful
I would like to detect what environment the site is currently running,

IE. local / staging / production

I've noticed there is a function detectEnvironment but not fully work out how to use this correctly.

Can anyone help out.

reason for this is to include the livereload script only if the environment is local

View Replies: View Best Answer
andrew replied on at Permalink Reply
This thread might be useful to you:


Basically you use the application/bootstrap/start.php file to map a local host name to an environment key, and then you create key.file.php in your application/config directory, and don't check that file into your source control.

e.g. if you map "andrew" to "andrew-hostname.whatever", whenever you're running on andrews-hostname.whatever, andrew.database.php will be used for database config, instead of database.php.
dpidan replied on at Permalink Best Answer Reply
I was trying to do almost the same thing.
You'll want to first make sure your host names are set up in /application/bootstrap/start.php. You might try PHP gethostname() to find your local host name, or, in your Shell, type: "hostname".

You can add as many hostnames to the arrays as you like - and as many environments as you like. Mine looks something like this:
        'local' => array(
        'stage' => array(
        'production' => array(

Finally, the magic that took nearly an hour of digging to find:
$env = Config::getEnvironment();
if ($env == 'local') {
    // add local development tools here