Benjamin Gonzalez
Benjamin Gonzalez

Reputation: 1343

How to set up different environments on Laravel 4?

I am trying to set up multiple environments on a Laravel 4 app, and naturally different Databases depending on which environment we are on.

For my local machine, I set up a virtual host for "local.elders.dev"

Unfortunately, for some reason the following code is not working.

$env = $app->detectEnvironment(array(
    'local' => array('http://local.elders.dev'),
));

Maybe I need to run an artisan command or something else. I feel I am close, but not quite there yet !

Thanks to all !

Upvotes: 4

Views: 6687

Answers (6)

Arne
Arne

Reputation: 6240

Another method is to use the name of the folder the project is in. This works in console and web. I found this to be the only reliable way if different environments are hosted on the same server.

$env = $app->detectEnvironment(array(
  'staging'     => strpos(getcwd(), '/staging')>-1, 
  'acceptance'  => strpos(getcwd(), '/acceptance')>-1, 
  'production'  => strpos(getcwd(), '/production')>-1, 
));

A big advantage is that you can move staging to production by simply renaming or copying the project folder without changing files, db records or environment variables.

Upvotes: 4

FredTheWebGuy
FredTheWebGuy

Reputation: 2595

Handling multiple environments on the same machine with Laravel 4x

What if you wanted to run multiple environments on the same machine with the same name- for example, a staging and production AND local environment?

There is a better solution for handling environments in Laravel 4x- and it can be done by adding a one liner to you vhosts file- or .htaccess:

Set local environment variable

In vhost or .htaccess add for your local installation, for staging, for example add:

SetEnv LARAVEL_ENV staging

and the same in your production .htaccess or vhost:

SetEnv LARAVEL_ENV production

Then the usual detectEnvironment() function in start.php.

$env = $app->detectEnvironment(function()
{
// Default to local if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: 'local';
});

We didn't forget local... and that's the the cool part-- your installation will default to local if neither environment variable is found in vhost or .htaccess, as they would be found in the other installations.

Upvotes: 3

Victor BV
Victor BV

Reputation: 1101

Laravel 4 detects the environments through the machine names specified in the "bootstrap/start.php" file.

For example, in my case the config becomes:

$env = $app->detectEnvironment(array(
  'local' => array('Victor.local', 'Victor-PC'),
));

This means that Laravel will use the 'local' environment settings for both machines: 'Victor.local' (a Mac) and 'Victor-PC' (Windows).

In order to know the current machine name, you can use the following PHP code:

<?php echo gethostname(); ?>

For each environment you can create a folder in app/config and replace the desired configuration files and properties.

Regards!

Upvotes: 0

wpjmurray
wpjmurray

Reputation: 8141

I know this is answered but for others looking for a solution...

My environment detection setting looks like this:

$env = $app->detectEnvironment(array(

// Development
// any machine name with the term "local" will use the local environment
'local' => array('*local*'),

// Stage
// any machine name with the term "stage" will use the stage environment
'stage' => array('*stage*')

// Production
// production is default, so we don't need to specify any detection

));

This is handy because it will work on any project as long as I use "local" for development (like "localhost", "localhost:8000", "my.app.local", etc.). Same goes for "stage". And production is default so anything without "local" or "stage" works for production.

Upvotes: 3

Benjamin Gonzalez
Benjamin Gonzalez

Reputation: 1343

OK ! I just solved the issue... The code was actually working ok ! The problem is that I was using

$_SERVER['DB1_HOST'] //for Pagodabox.

Of course this was not set on my local environment, which pretty much broke the app...

I fixed by simply doing :

isset($_SERVER['DB1_HOST']) ? $_SERVER['DB1_HOST'] : '';

Thanks to @jeroen and @theshiftexchange :)

Upvotes: 1

Jeroen
Jeroen

Reputation: 13257

Try replacing it with 'local.elders.dev', I'm not 100% sure but it's probably matching hostnames, not full paths.

Upvotes: 0

Related Questions