What is the file . env in Laravel 5?

Asked

Viewed 6,589 times

8

  1. What’s the file for .env?

  2. In the app.php has env => env('APP_ENV','production'). What would that be Production? What other string can go there?

  3. And the var APP_ENV? What does it mean?

2 answers

13

In the file . env the Laravel environment settings are stored.

According to the documentation:

Environment Settings

It is very useful to have different configuration values based on the environment in which the application is running. For example, you may want to use different cache drivers locally and another on the production server. This is very easy using the environment-based settings.

To make this easy, Laravel uses Vance Lucas' PHP Dotenv Library (vlucas). In an initial installation of Laravel, the main directory of the application will contain the file env.example. If you installed Laravel via Composer, this file will be automatically copied to .env. However, you can copy or rename it manually.

All variables listed in this file will be loaded into the PHP super global variable $_ENV when your application receives a request. You can use the env utility function to pick up these values. If you have reviewed the Laravel configuration files, you may have noticed that many of the options already use this utility function.

You are free to modify your environment variables as you wish, for your local application or for your production server. However, your file .env should not be versioned, because each developer / server may need a different configuration from that file.

If you are developing as a team, you can include the file .env.example and add there some comments on the file configuration, so that each developer clearly understands which settings they should make on their own .env to run your application.

Accessing the Current Application Environment

You can access the current environment of your application by the App facade environmentdo method:

$environment = App::environment();

You can also pass arguments to the Nvironment method to verify that the current environment is what was passed by argument. You can even pass multiple environments or call the method multiple times if necessary:

if (App::environment('local')) {
    // Aqui instruções para rodar no ambiente local
}

if (App::environment('local', 'staging')) {
    // Aqui instruções para rodar no ambiente local OU staging
}

An instance of the application can also be accessed by the app utility function:

$environment = app()->environment();

3

What is the file . env?

The archive .env is an easier way to load custom configuration variables that your app will need to have.

This means that you will have to modify the file outside of the project, and all the environment variables will always be defined no matter how you run your code this is reflected even in built-in PHP servers.

What would this Production be?

The Production is the environment your application is in, can also be defined as local.

And var APP_ENV? What does it mean?

ENV comes from Environment, meaning Environment. Every configuration created generates a key with this value to define the environment in which your project is located.

Details

Browser other questions tagged

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