Home > PHP Framework > Laravel > What should I do if env cannot obtain data in laravel?

What should I do if env cannot obtain data in laravel?

WBOY
Release: 2022-06-02 17:14:52
Original
4186 people have browsed it

In laravel, you can use the "php artisan config:clear" statement to clear the config cache to solve the problem that env cannot obtain data; the reason for this problem is that the cache configuration file reads the environment variables through the env function, which may This will result in the environment variable not being read, so null is returned.

What should I do if env cannot obtain data in laravel?

The operating environment of this article: Windows 10 system, Laravel version 5.4, Dell G3 computer.

What to do if the env in laravel cannot obtain data

Solution:

Clear config cache command

php artisan config:clear
Copy after login

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{
                (newDotenv($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.

[Related recommendations: laravel video tutorial]

The above is the detailed content of What should I do if env cannot obtain data in laravel?. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template