Home  >  Article  >  PHP Framework  >  The relationship between the .env file in the laravel framework and the configuration file in the config directory

The relationship between the .env file in the laravel framework and the configuration file in the config directory

齐天大圣
齐天大圣Original
2020-12-11 21:50:161894browse

Maybe many children, like me, were confused by the .env file and the configuration files in the config directory when reading the document. What is the relationship between them. Today, let’s take a look at their previous relationship.

The relationship between the .env file and the config directory configuration file

First of all, the document sayslaravelAll configuration files are saved in the files in the config directory. So what exactly is the .env file in the root directory used for? Let's open the .env file and config/app.php.

# .env文件
APP_NAME=Laravel
……    
# app.php文件
'name' => env('APP_NAME', 'Larave'),

Seeing this, I can basically guess the general meaning: If the configuration file configuration item in the config directory uses env function, then if this option is configured in the .env file, the value in the .env file will be used, otherwise the default value set in the configuration file will be used.

Next, let’s verify my guess. First write the test code:

return config('app.name');

Then, change the APP_NAME of the .env file to boy, and change the default value in the app.php file to girl

APP_NAME=boy
……
'name' => env('APP_NAME', 'girl'),

Found an interesting problem, test The result returned by the code is still Laravel. Then, I restarted php artisan serve, and then refreshed again and the result was boy. This verified that my guess was correct, but it led to another speculation: If the configuration value used is in the .env file, then modifying the value in the .env file will not take effect immediately and must be restarted. Valid

In order to verify another guess of mine, I deleted the APP_NAME line in .env and then restarted. The return result of the test is girl. Next, I changed girl to girl1, then refreshed the browser, and the return value was girl1. So my conclusion is verified to be correct.

To summarize here:

  • If the configuration file configuration items in the config directory use the env function, then if this option is configured in the .env file, use . env file, otherwise the default value set in the configuration file will be used

  • If the configuration value used is in the .env file, then modifying the value in the .env file will not be effective immediately , you must restart php artisan serve for it to be effective

The above is the detailed content of The relationship between the .env file in the laravel framework and the configuration file in the config directory. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn