Home >PHP Framework >Laravel >The role of .env file in laravel
I wonder if you have ever considered this question: Now that there is a configuration file, what is the role of this .env file?
You must have had this experience. The local environment and the server environment are different, and some configuration values will be different, such as different database configuration information, different domain names, etc. This will cause some problems. If you use a version control system, then the server side pulls the latest code, and the server side configuration file will become the local development environment. Then you need to look at them one by one and then modify them. Isn’t it very troublesome! It is also possible that your local configuration information will be seen by other development partners (maybe your local database information is also very important, and you don’t want others to know it)
.env file is to solve this problem of. We can put the same server and local environment configuration items in the configuration file, and put different configuration items and sensitive information in the .env file. Also note that the .env file is not placed in the version control system .
In addition, you should notice that there is also an .env.example file in the root directory. It is generally recommended to place this file in a version control system to let other developers know that you should make a copy of this file and change its name to .env. Then set the values inside according to your own environment.
If you are like me, there are three different environments: development environment, test environment, and online environment. Then you should also create another .env.testing file. This file will overwrite the values in the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option.
Super global variable $_ENV
.env file contains all the variables listed, which will be loaded into the super global variable $_ENV of PHP, and the corresponding value can be retrieved through this variable.
Determine the current system environment
The current environment of the application is determined by APP_ENV in the .env file. You can use the App::environment() method to get the value of this option. Such as:
if ('local' == App::environment()) { // 本地环境 }
The above is the detailed content of The role of .env file in laravel. For more information, please follow other related articles on the PHP Chinese website!