1
votes

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?

2

2 Answers

3
votes
php artisan --env=staging

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

-1
votes

Not sure if you've tried this but I would use the hostname instead of the domain. In your staging server using your CLI type hostname and it will tell you the hostname of your server.

You can add this to bootstrap/start.php

$env = $app->detectEnvironment(array(

    'local' => array('homestead'), // Change this to your local machine hostname.
    'staging' => array('staging_hostame'), // staging server
    'production' => array('your-production-machine-name'),

));

Let me now if you need further help on this :)