Exploring the problem of laravel using the ENV function to read the environment variable null _php tips

Source: Internet
Author: User

Find problems

In the Laravel project, if you execute a php artisan config:cache command to cache the configuration file, in Tinker (Tinker is an interactive command-line interface that is Laravel), use the env function to read the value of the environment variable null only if the php artisan config:clear cleanup configuration The cache can be read, what is this for?

A probe into the Earth

Open the. env file to see that these are all of a value:

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

As shown in the figure:

Why?

In Laravel, if you execute php aritisan config:cache a command, Laravel will consolidate all of the configuration files in the App/config directory into a cached configuration file to bootstrap/cache/config.php, and each profile can be env function to read the environment variable, which is readable here. But once you have this cache profile, you can use the function elsewhere to env read the environment variable, so return null .

Let's take a look at this piece of 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 ();
 Invalidpathexception $e) {
  //
 }
 }
}

This method runs after the framework is started, and this code shows that if there is a cached configuration file, the environment variable is not set, the configuration reads the cache profile, and the environment variable is not read again.

Therefore, in the configuration file, elsewhere in the App/config directory, read the configuration do not use env functions to read environment variables, so that once you execute php artisan config:cache , the env function will not work. All the environment variables to be used are read through the env in the configuration file of the App/config directory, where the environment variables are used uniformly to read the configuration file instead of using the env function read.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.