Laravel升级后,.env变量未定义索引

I'm upgrading Laravel to 5.2 from 5.1. When i refere to variable API_DOMAIN in .env file using $_ENV $_ENV['API_DOMAIN'] I get an error saying Undefined index: API_DOMAIN". Am I missing something here? should i do something after composer update?

评论
  • Sim
    Sim 回复

    您不应该在应用程序中直接使用环境值,我会创建一个新的配置文件或使用现有的配置文件并在其中添加值,这样您就可以缓存配置。

    Lets use your API_DOMAIN as an example:

    .env文件

    API_DOMAIN=www.mydomain.com
    

    配置文件

    config/api.php for example

    <?php
    
    return [
        'domain' => env('API_DOMAIN'),
    ];
    

    Now you can use the config helper to use this variable in your application:

    $value = config('api.domain');
    

    You should never use the env() helper outside of config files, because when you cache your config, env() will return null.

  • ~昵称
    ~昵称 回复

    Try env helper instead, env('API_DOMAIN')