This is how I do set my environment:
Create a .environment
file in the root of your application and define your environment and add your sensitive information to it:
<?php
return array(
'APPLICATION_ENV' => 'development', /// this is where you will set your environment
'DB_HOST' => 'localhost',
'DB_DATABASE_NAME' => 'laraveldatabase',
'DB_DATABASE_USER' => 'laraveluser',
'DB_DATABASE_PASSWORD' => '!Bassw0rT',
);
Add it to your .gitignore
file, so you don't risk having your passwords sent to Github or any other of your servers.
Right before $app->detectEnvironment
, in the file bootstrap/start.php
, load your .environment
file to PHP environment:
foreach(require __DIR__.'/../.environment' as $key => $value)
{
putenv(sprintf('%s=%s', $key, $value));
}
And then you just have to use it:
$env = $app->detectEnvironment(function () {
return getenv('APPLICATION_ENV'); // your environment name is in that file!
});
And it will work everywhere, so you don't need to have separate dirs for development and production anymore:
<?php
return array(
'connections' => array(
'postgresql' => array(
'driver' => 'pgsql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_DATABASE_NAME'),
'username' => getenv('DB_DATABASE_USER'),
'password' => getenv('DB_DATABASE_PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
),
);
Note that I don't set a fallback:
return getenv('APPLICATION_ENV') ?: 'local';
Because, if I don't set the file, I want it to fail on every server I deploy my app to.
Laravel leverages something similar, you create .env
files in the root of your application:
.env.local.php // this file will appear only in your local environment, not in production
.env.staging.php // for staging
.env.php // for production
In those files you add your sensitive information
<?php
return array(
'DB_HOST' => 'localhost',
'DB_USER' => 'root',
'DB_PASS' => '1023809182root@pass',
);
Create your files separated by environment:
app/config/local/database.php
app/config/staging/database.php
app/config/database.php
An then in your files, or anywhere in your application, you can access your sensitive data via $_ENV or getenv():
$_ENV['DB_HOST']
$_ENV['DB_USER']
$_ENV['DB_PASS']
Example:
'postgresql' => [
'driver' => 'pgsql',
'host' => 'localhost',
'database' => getenv('DB_HOST'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
But you still have to set the environment name, which might be a problem in your case.
Don't forget to add those files to your .gitignore file, so you don't risk sending them to your github.
Documentation: http://laravel.com/docs/configuration#protecting-sensitive-configuration