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.