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 !

Laurence
  • 58,936
  • 21
  • 171
  • 212
Benjamin Gonzalez
  • 1,343
  • 2
  • 9
  • 11
  • I don't know what "for some reason it isn't working" menas. Got any sort of PHP error to show off? :) – Phil Sturgeon Jan 25 '13 at 21:22
  • Nope, it just doesnt recognise my local environment... I have a database.php file on a 'config/local' folder, but Laravel is still using the production settings for db.. – Benjamin Gonzalez Jan 25 '13 at 21:24
  • Type var_dump($_SERVER); die(); before $env, and see what your server name is called - then set it to that – Laurence Jan 26 '13 at 07:55
  • And also check this link for a possible answer: http://stackoverflow.com/q/13860283/1317935 – Laurence Jan 26 '13 at 07:58

6 Answers6

4

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.

Arne
  • 6,140
  • 2
  • 21
  • 20
3

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.

wpjmurray
  • 8,141
  • 1
  • 17
  • 17
  • 1
    Murzeb: I'm working in my local machine with WAMP. I have added the above coding in start.php. But the environment value returns production. How can I get local environment? My local URL is http://localhost/laravel-app/public/home. Please advice.. – user2003356 Apr 08 '14 at 09:52
3

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.

FredTheWebGuy
  • 2,546
  • 3
  • 27
  • 34
  • This is my preferred technique, with one difference: I prefer to default to production. If we default to anything else, then a failure to configure in production could leak internals - like debug dumps, stack traces, etc, - that are likely enabled in pre-production environments. – David Weinraub Jul 05 '16 at 09:13
  • Also, remember to prepend the LARAVEL_ENV setting to command-line invocations, either via bash `export` or manually on invocation using something like `LARAVEL_ENV=staging php /path/to/command.php` – David Weinraub Jul 05 '16 at 09:15
1

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 :)

Benjamin Gonzalez
  • 1,343
  • 2
  • 9
  • 11
0

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

Jeroen
  • 13,056
  • 4
  • 42
  • 63
  • What about `'*.dev'`? If even that isn't matching, there's something wrong with the way you set up the database config file. – Jeroen Jan 25 '13 at 21:31
  • @jeroden You were right. The local environment was being set correctly... I willanswer my own question with how I manage to fix it – Benjamin Gonzalez Jan 26 '13 at 10:21
0

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!

Victor BV
  • 1,051
  • 13
  • 9