Skip to content

Instantly share code, notes, and snippets.

@antonioribeiro
Last active January 30, 2016 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save antonioribeiro/8429426 to your computer and use it in GitHub Desktop.
Save antonioribeiro/8429426 to your computer and use it in GitHub Desktop.
Laravel Environmental

Create a .environment file in the root of your application and define your environment and gitignore it:

return array(

      'LARAVEL_ENV' => 'development',

      'POSTGRESQL.HOST' => 'localhost',
      'POSTGRESQL.DATABASE_NAME' => 'laraveldatabase',
      'POSTGRESQL.DATABASE_USER' => 'laraveluser',
      'POSTGRESQL.DATABASE_PASSWORD' => '!Bassw0rT',

);

Load it in Laravel right before $app->detectEnvironment():

foreach(require __DIR__.'/../.environment' as $key => $value) 
{
    putenv(sprintf('%s=%s', $key, $value));
}

Then just use it everywhere:

 <?php

 return array(

      'connections' => array(

           'postgresql' => array(
                'driver'   => 'pgsql',
                'host'     => getenv('POSTGRESQL.HOST'),
                'database' => getenv('POSTGRESQL.DATABASE_NAME'),
                'username' => getenv('POSTGRESQL.DATABASE_USER'),
                'password' => getenv('POSTGRESQL.DATABASE_PASSWORD'),
                'charset'  => 'utf8',
                'prefix'   => '',
                'schema'   => 'public',
           ),

      ),

 );

Better than use it all in the .htaccess file, IMO.

@martindilling
Copy link

Any reason to use this over environment configurations like here: http://laravel.com/docs/configuration#environment-configuration ?

@antonioribeiro
Copy link
Author

Those are my reasons, but might not be good for everyone, of course:

  1. one file to rule them all.

  2. environment name set in .env file, not by extension.

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