Home  >  Q&A  >  body text

Is laravel's .env file only valid when called within the config folder?

The author writes a Artisan command and calls the getenv method in the command file under App\Console to obtain .env configuration, the result returns null;

But by getting the configuration of .env in config/services.php, and then in the command file under App\Console By calling the config method, you can get the desired environment variable value. Why is this?

给我你的怀抱给我你的怀抱2661 days ago874

reply all(2)I'll reply

  • 黄舟

    黄舟2017-06-13 09:26:33

    The official suggestion is to call env in config and call config elsewhere.

    This is actually a big pitfall. When I first wrote Laravel, I found that the env could not be read in the controller. (It’s not that it can’t be read 100%, it’s just that it can’t be read after executing php artisan config:cache)

    Caching And Env

    If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

    If you are calling env from within your application, it is strongly recommended you add proper configuration values ​​to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

    Excerpted from https://laravel.com/docs/5.2/...

    reply
    0
  • 天蓬老师

    天蓬老师2017-06-13 09:26:33

    Laravel has an auxiliary function env() Yeah = =

    reply
    0
  • Cancelreply