How to configure Laravel 4 to automatically detect which environment I am in (production and development)?

1

In Laravel 4 , we have a configuration file in app/config/database.php . And in the app/config/local/database.php folder you have another file.

The Laravel 4 has a mechanism to be able to detect in which environment we are (it seems to me that it is through the person's computer name), so we can determine if it will use database.php in production and local/database.php during development.

With setting Laravel 4 to detect the environment according to the host I'm using?

I have a virtual host called laravel on my machine and would like it when automatically using Laravel on that host to automatically be determined as a development environment.

Is there a way to do this in Laravel 4 ?

    
asked by anonymous 17.02.2016 / 12:51

1 answer

1

You should use the Illuminate\Foundation\Application::detectEnviroment() method. It is that object that is stored in the global variable $app . You should pass as an argument to this method a array or a função anônima . In the case of the anônima function, you should make a condition that returns a string containing the word local for development environments, test for environments of test and production for the environment.

In this case, to use the configuration in config/local/database.php , we will check the host and then return local if it is laravel or localhost .

This is possible in laravel 4 through a file called bootstrap/start.php , where this setting can be made including the following code:

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

    $host = $app['request']->server('HTTP_HOST');

    if (in_array($host, ['localhost', 'laravel'])) {

        return 'local';

    } 

    return 'production';

});
    
17.02.2016 / 13:06