Skip to content

Instantly share code, notes, and snippets.

@reinink
Last active August 29, 2015 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reinink/6229c9fca8476e720951 to your computer and use it in GitHub Desktop.
Save reinink/6229c9fca8476e720951 to your computer and use it in GitHub Desktop.
Add environment var config to Laravel 4
APP_ENV=local
APP_DEBUG=true
{
"require": {
"vlucas/phpdotenv": "~1.1"
}
}
<?php
// app/config/database.php
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_DATABASE'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
<?php
// bootstrap/start.php
$env = $app->detectEnvironment(function () {
try {
Dotenv::load(__DIR__.'/../');
} catch (Exception $e) {
// No .env file found
}
return getenv('APP_ENV') ? getenv('APP_ENV') : 'production';
});
@joecohens
Copy link

And a nice helper.

if ( ! function_exists('env'))
{
    /**
     * Gets the value of an environment variable. Supports boolean, empty and null.
     *
     * @param  string  $key
     * @param  mixed   $default
     * @return mixed
     */
    function env($key, $default = null)
    {
        $value = getenv($key);

        if ($value === false) return value($default);

        switch (strtolower($value))
        {
            case 'true':
            case '(true)':
                return true;

            case 'false':
            case '(false)':
                return false;

            case 'null':
            case '(null)':
                return null;

            case 'empty':
            case '(empty)':
                return '';
        }

        return $value;
    }
}

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