How do I set up Laravel 4 to automatically detect which environment I’m in (production and development)?

Asked

Viewed 56 times

1

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

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

With setting up the Laravel 4 to detect the environment according to the host that I’m using?

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

There is a way to do this in the Laravel 4?

1 answer

1


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

In this case, to use the configuration present in config/local/database.php, let’s check the host and then return local if the same is laravel or localhost.

This is possible in Laravel 4 through a file called bootstrap/start.php, where this configuration 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';

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.