


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
As shown in the picture:
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) { // } } }
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}}?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
