Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeingTomGreen/7913876 to your computer and use it in GitHub Desktop.
Save BeingTomGreen/7913876 to your computer and use it in GitHub Desktop.
A quick way of reinstating the older Laravel 4 environment detection. As of 4.1 host name based environment detection has been removed due to security concerns, but some people may wish to add this back in. See more here: https://github.com/laravel/framework/commit/02618b4190c08e1fe6e138eadb95473c92da6718
<?php
// An array of environments, exactly as old syntax
$envs = [
'local' => ['Alex','Al', 'Tom-PC', 'Tom', 'mgc.dev'],
'local_mamp' => ['navi-mini.home', 'aaron-whiffins-mac-mini.home'],
'production' => ['mygiftclues.com'],
'staging' => ['giftclue.co.uk']
];
// Grab our hostname and machine name
// From the command line HTTP_HOST won't exist so ignore that
$hostName = isset($_SERVER["HTTP_HOST"]) ? parse_url($_SERVER["HTTP_HOST"])['path'] : '';
$machineName = gethostname();
// Loop through each of the possible environments
foreach ($envs as $key => $environment)
{
// Now check if either the hostname or machine name match an element in the environment array
if (in_array($hostName, $environment) or in_array($machineName, $environment))
{
// If so set this as the environment
return $key;
}
}
@valeryan
Copy link

You example works pretty good thanks, I think it might be cool to also replicate the wildcard feature. Something like this:

// An array of environments, exactly as old syntax
// with support for wildcards
// first match wins so order highest priority at top
$envs = [    
  'beta' => ['beta.*'],
  'local' => ['*.lo', '*.local', 'your-machine-name'],
  'production' => ['*.example.com']
];

// Grab our hostname and machine name
// From the command line HTTP_HOST won't exist so ignore that
$hostName = isset($_SERVER["HTTP_HOST"]) ? parse_url($_SERVER["HTTP_HOST"])['path'] : '';
$machineName = gethostname();

// Loop through each of the possible environments
foreach ($envs as $key => $environment)
{
    // build a regex patter from values
    $regex = '('.implode(')|(',$environment).')';
    $regex = '#' . str_replace(array('*'), array('[a-z]+'), $regex) . '#';
    // check regex against hostname and machinename
    if (preg_match($regex, $hostName) || preg_match($regex, $machineName))
    {
        // return key of the first match we find
        return $key;
    }

}
// default to production if nothing matched
return 'production';

@rs3d
Copy link

rs3d commented Apr 1, 2014

This supports wildcards with $_SERVER["HTTP_HOST"] and hostname() as cli-fallback.

$env = $app->detectEnvironment(function(){
$hostName = isset($_SERVER["HTTP_HOST"]) ? parse_url($_SERVER["HTTP_HOST"])['path'] : gethostname();

$availableEnvironments = array(
    'development' => array('*berlin.wg', '*dev*', 'ubuntu'),
    'staging' => array('*staging*', ),
);

foreach ($availableEnvironments as $env => $hosts) {
    foreach ($hosts as $key => $value) {
        if (str_is($value, $hostName )) {
            return $env;
        }
    }
}
return 'production';

});

@BeingTomGreen
Copy link
Author

If anybody sees this, Laravel 5 has started using vlucas/phpdotenv.

You could roll this into your Laravel 4 install without too much work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment