Laravel 4.1 defining several environments for web servers and amp; Terminal

advertisements

Okay, I get the multi-environments from the Laravel Docs.

Here's my setup:

  • local environment
  • staging server environment
  • production server

The local environment has a different hostname from the production server obviously, however the staging server is a subdomain on the production server (so dev.myapp.com vs myapp.com). I also use myapp.dev as my virtual hostname locally.

I've set the following to switch the enviornments:

$env = $app->detectEnvironment(function(){

if(strpos($_SERVER['HTTP_HOST'],'.dev') !== false) || strpos($_SERVER['HTTP_HOST'],'dev.') !== false) {
    return 'local';
}
return 'production';

});

This almost works. The problem is I have a separate database setup for my dev/staging environments so that we're not showing or manipulating live data on the staging environment. This falls down in terminal. When I run php artisan env (after a warning message about $_SERVER['HTTP_HOST'] not being set) it shows as production.

That means it won't run migrations on the correct database through terminal.

What's a better way to reference this?


php artisan --env=staging

is what you need, and add isset($_SERVER['HTTP_HOST']) to the detection code, otherwise cli will complain.