Support the ongoing development of Laravel.io →
Configuration Requests
Last updated 2 years ago.
0

Environment detection by URL was removed in 4.1 (see upgrade guide "Environment Detection Updates").

Last updated 9 years ago.
0

Hi kokokurak,

If you want detect by domain name you can do it like this:


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

    $env = '';
    
    switch($_SERVER['HTTP_HOST']){
        case 'test.localhost.dev':
            $env = 'dev';
            break;
    }
  
    return $env ?: 'production';

});

But it's not the goo way to detect the environment because:

. The environment is not detected properly in CLI . Domain spoofing

To configure properly one application I use this way:

$env = $app->detectEnvironment(function()
{
    $env = getenv('LARAVEL_ENV');

    if(empty($env)){
        if(file_exists(__DIR__.'/../.env')){
            $env = trim(file_get_contents(__DIR__.'/../.env'),"\n");
        }
    }

    return $env ?: 'local';

});

I use getenv to get the environment name put in the vhost configuration LARAVEL_ENV.

SetEnv LARAVEL_ENV staging 

If you can put environment variable on your vhost you can put a .env file at the root of your application en put the environment name.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

kokokurak kokokurak Joined 21 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.