Home > php教程 > PHP开发 > body text

Explore the problem that Laravel uses the env function to read environment variables that are null

高洛峰
Release: 2017-03-24 15:11:45
Original
2864 people have browsed it

Discover the problem

In the Laravel project, if you execute the php artisan config:cache command to cache the configuration file, in Tinker (Tinker is an interactive command line interface that comes with Laravel), The value of the environment variable read using the env function is null. It can be read only after clearing the configuration cache by executing php artisan config:clear. Why is this?

Let’s find out

Open the .env file and see, these are all valuable:

APP_ENV=local
APP_KEY=base64:JHE5bOkRg283uT0n1Zq/GgvGEer8ooYiB42/wIcCyvo=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://www.tanteng.me
  
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tanteng.me
DB_USERNAME=homestead
DB_PASSWORD=secret
Copy after login


As shown in the picture:

Explore the problem that Laravel uses the env function to read environment variables that are null

What is the reason?

In Laravel, if you execute the php aritisan config:cache command, Laravel will "compile" and integrate all the configuration files in the app/config directory into a cache configuration file into bootstrap/cache/config.php. Each configuration file can read environment variables through the env function, which can be read here. But once you have this cache configuration file, you cannot read the environment variables using the env function elsewhere, so null is returned.

Let's take a look at this code, Illuminate/Foundation/Bootstrap/DetectEnvironment. php line 18:

public function bootstrap(Application $app)
{
 if (! $app->configurationIsCached()) {
 $this->checkForSpecificEnvironmentFile($app);
  
 try {
  (new Dotenv($app->environmentPath(), $app->environmentFile()))->load();
 } catch (InvalidPathException $e) {
  //
 }
 }
}
Copy after login

This method will run after the framework is started. This code shows that if a cache configuration file exists, environment variables will not be set. The configuration will read the cache configuration file instead of Will read environment variables again.

Therefore, when reading configuration files elsewhere in the app/config directory, do not use the env function to read environment variables. In this way, once you execute php artisan config:cache, the env function will not work. . All environment variables to be used are read through env in the configuration file in the app/config directory. If environment variables are used elsewhere, the configuration file is read uniformly instead of using the env function.

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more related articles exploring the problem of using the env function to read environment variables as null in Laravel, please pay attention to the PHP Chinese website!

Related articles:

In the Laravel framework, what is the difference between {{url}} and {{asset}}?

##laravel The use of named routes in

A note about Laravel Route redirection

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!