Laravel 5.6 env('APP_BASE_URL')返回null

dan*_*l14 4 environment-variables laravel laravel-5.6

在暂存和生产环境中,当我尝试从.env文件获取自定义变量时,它返回null.我尝试创建新的APP键并清除所有类型的缓存,但结果是一样的.

APP_NAME="App - Staging"
APP_ENV=staging
APP_KEY=<HIDDEN_KEY>
APP_DEBUG=true
APP_LOG_LEVEL=none
APP_URL=https://staging.app.com

APP_BASE_URL="https://app-staging.app.com"
Run Code Online (Sandbox Code Playgroud)

清除缓存

php artisan config:clear                     
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:cache
Run Code Online (Sandbox Code Playgroud)

但是在刀片视图中使用以下内容时什么都没有

env('APP_BASE_URL') returns null
Run Code Online (Sandbox Code Playgroud)

pse*_*ime 6

这是因为你运行了php artisan config:cache.如果您使用的是config:cache,则只应在配置文件中进行env()调用.

请参见:https://laravel.com/docs/5.6/configuration#configuration-caching


dan*_*l14 6

这篇文章解决了我的问题,我在 config/app.php https://laracasts.com/discuss/channels/laravel/accessing-custom-environment-variable 中定义了自定义配置

/*
|-------------------------------------------------------------------------
| Custom config variables
|-----------
|
 */
'base_url' => env('APP_BASE_URL', 'http://localhost'),
Run Code Online (Sandbox Code Playgroud)

然后在 .env 中我定义了:

APP_BASE_URL="https://app-staging.app.com"
Run Code Online (Sandbox Code Playgroud)

最后清除缓存,工作。

在刀片视图中

{{ config('app.base_url') }}
Run Code Online (Sandbox Code Playgroud)