Thelonias
Thelonias

Reputation: 2935

How to replace environment-specific config, not cascade

I want to have a local development environment configuration for my database.php. I've created a new config director in my app and copied the database.php file into it. I'm using MongoDB and on production, I'm using a replica set with multiple servers, but locally, I'm just running a single server, so my connection info for production has more options:

Production:

    ...
'connections' => array(

    'mongodb' => array(
        'driver'   => 'mongodb',
        'host'     => array('mongoDBA', 'mongoDBB'),
        'port'     => 27017,
        'username' => 'myUserName',
        'password' => 'myPassword',
        'database' => 'theDatabase',
        'options'  => array('replicaSet' => 'myReplicaSet')
    )
),

Local:

    ...
'connections' => array(

    'mongodb' => array(
        'driver'   => 'mongodb',
        'host'     => 'localhost',
        'port'     => 27017,
        'database' => 'theDatabase'
    )
),

The problem is, when my local environment config loads, it merges the "connections" array. I want a way to completely replace the "mongodb" connection, so it's either one or the other, not both.

How can I accomplish this?

Upvotes: 0

Views: 162

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

Create different set of configuration files:

app/config/local/database.php
app/config/production/database.php

And delete the file (or what you don't want of it):

app/config/database.php

And the you have to set your environment as

local
development

EDIT

This is how I do set my environment flawlessly, so I don't have to deal with hostnames and still don't get my local environment conflict with staging and production.

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.

Upvotes: 1

Related Questions