Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 544
Benjamin Gonzalez Avatar asked Dec 21 '22 10:12

Benjamin Gonzalez


2 Answers

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.

like image 166
Arne Avatar answered Dec 22 '22 22:12

Arne


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.

like image 21
wpjmurray Avatar answered Dec 22 '22 22:12

wpjmurray