Laravel 5.6 env('APP_BASE_URL') 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51061869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.6 env('APP_BASE_URL') returns null
提问by danyal14
In staging and production environment, when I try to get my custom defined variable from .env file, it returns null. I have tried creating new Key for APP and cleared all sort of caches, but result is same.
在登台和生产环境中,当我尝试从 .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"
Clearing Cache
清除缓存
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:cache
But when using following in blade view gets nothing
但是当在刀片视图中使用以下时,什么也得不到
env('APP_BASE_URL') returns null
回答by pseudoanime
It is because you have run php artisan config:cache. If you are using config:cache, your env() calls should only be made in your config files.
这是因为您已经运行了 php artisan config:cache。如果你使用 config:cache,你的 env() 调用应该只在你的配置文件中进行。
See here: https://laravel.com/docs/5.6/configuration#configuration-caching
请参阅此处:https: //laravel.com/docs/5.6/configuration#configuration-caching
回答by danyal14
This post solved my issue, I defined custom config in config/app.php https://laracasts.com/discuss/channels/laravel/accessing-custom-environment-variable
这篇文章解决了我的问题,我在 config/app.php https://laracasts.com/discuss/channels/laravel/accessing-custom-environment-variable 中定义了自定义配置
/*
|-------------------------------------------------------------------------
| Custom config variables
|-----------
|
*/
'base_url' => env('APP_BASE_URL', 'http://localhost'),
Then in .env I definded:
然后在 .env 中我定义了:
APP_BASE_URL="https://app-staging.app.com"
Finally cleared the cache, worked.
最后清除缓存,工作。
In blade view
在刀片视图中
{{ config('app.base_url') }}
回答by Parithiban
Yes it will return only null
是的,它只会返回 null
If you execute the config:cache
command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.
如果config:cache
在部署过程中执行该命令,则应确保仅从配置文件中调用 env 函数。一旦配置被缓存,.env 文件将不会被加载,所有对 env 函数的调用都将返回 null。
See the laravel documentation https://laravel.com/docs/5.6/configuration#configuration-caching
请参阅 laravel 文档https://laravel.com/docs/5.6/configuration#configuration-caching