php Laravel 5 - env() 总是返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43243732/
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 - env() always returns null
提问by Fusion
I try to find out why my env()
helper always returns null
. This causes trouble especially in app.php
file, where are env()
helpers widely used by default. Perhaps any mysterious server setting?
我试图找出为什么我的env()
帮手总是回来null
。这会导致麻烦,尤其是在app.php
文件中,env()
默认情况下,助手广泛使用。也许任何神秘的服务器设置?
My env file:
我的环境文件:
APP_ENV=production
APP_KEY=base64:mymagickey=
APP_DEBUG=false
APP_LOG_LEVEL=info
APP_URL=http://www.example.com
etc...
EDIT - I tried following:
编辑 - 我试过以下:
php artisan cache:clear
php artisan view:clear
php artisan config:cache
and ofcourse, i am using env
helper like this: env('APP_ENV')
当然,我正在使用这样的env
助手:env('APP_ENV')
But still no success. The wierd part is, that $_ENV
php variable contains every single variable from .env
file.
但仍然没有成功。奇怪的部分是,该$_ENV
php 变量包含.env
文件中的每个变量。
回答by Yevgeniy Afanasyev
env(...)
function will not work after you cached the config. (starting from laravel 5.2 till current 5.7)
env(...)
缓存配置后,该功能将不起作用。(从 laravel 5.2 到当前的 5.7)
The Laravel documentationsays
If you are using the
config:cache
command during deployment, you must make sure that you are only calling theenv
function from within your configuration files, and not from anywhere else in your application.
如果
config:cache
在部署期间使用该命令,则必须确保仅从env
配置文件中调用该函数,而不是从应用程序中的任何其他位置调用该函数。
So the correct answer would be to
所以正确的答案是
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.
如果您从应用程序中调用 env,强烈建议您将适当的配置值添加到配置文件中,然后从该位置调用 env,从而允许您将 env 调用转换为配置调用。
And I quoted it from the same documentation
我从同一个文档中引用了它
But for a quick fix this will do:
但是为了快速修复,这将执行以下操作:
php artisan config:clear
php artisan config:clear
And now it should be clear why, when you tried config:cache
, it did not help, even though it clears the config prior to caching.
现在应该清楚为什么当您尝试config:cache
时它没有帮助,即使它在缓存之前清除了配置。
回答by sh6210
Hope this command will save you
希望这个命令可以拯救你
php artisan config:clear
php工匠配置:清除
回答by Learner
Five most important commands if your Laravel is not working as expected after some modifications in .env or database folder or because of any other modifications. Here is full explanation: https://www.youtube.com/watch?v=Q1ynDMC8UGg
如果您的 Laravel 在 .env 或数据库文件夹中进行了一些修改或由于任何其他修改后无法按预期工作,那么五个最重要的命令。这是完整的解释:https: //www.youtube.com/watch?v=Q1ynDMC8UGg
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
composer dump-autoload
回答by DIGITALCRIMINAL
Use \Config::get('app.env');
instead of env(APP_ENV);
because you're going to get the same error eventually and that's not good for a live website.
使用\Config::get('app.env');
而不是env(APP_ENV);
因为您最终会遇到相同的错误,这对实时网站不利。
If you want to add custom variables from your ENV, go into your config app and find this:
如果您想从您的 ENV 添加自定义变量,请进入您的配置应用程序并找到以下内容:
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services your application utilizes. Set this in your ".env" file.
|
*/
'env' => env('APP_ENV', 'production'),
add a new line under "'env' => env('APP_ENV', 'production'),
", so for example, it could be the following:
在“ 'env' => env('APP_ENV', 'production'),
”下添加一个新行,例如,它可能是以下内容:
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services your application utilizes. Set this in your ".env" file.
|
*/
'env' => env('APP_ENV', 'production'),
'key' => env('APP_KEY'),
You can call the "key" variable like this:
您可以像这样调用“key”变量:
\Config::get('app.key');
Whenever you add a new variable like "key" to the app env, you'll need to use config:cache
to reset the cache.
每当您向应用程序环境中添加像“key”这样的新变量时,您都需要使用它config:cache
来重置缓存。
回答by vpdeva
It is a ".env" known bug which can be solved with:
这是一个“.env”已知错误,可以通过以下方式解决:
php artisan config:cache
回答by Santosh Kumar
The following Command worked for me.
I accidently cleared all the cache files, So env('something')
was returning null.
以下命令对我有用。我不小心清除了所有缓存文件,所以env('something')
返回null。
php artisan optimize:clear
回答by Sachithd
If the accepted answer doesn't fix the issue check if .env file has correct read and write permission given. If the file cannot be read by the framework / library then the values will always be null. I've come across this issue on one of my Lumen projects but after changing the file permissions it worked.
如果接受的答案不能解决问题,请检查 .env 文件是否具有正确的读写权限。如果框架/库无法读取文件,则这些值将始终为空。我在我的一个 Lumen 项目中遇到了这个问题,但在更改文件权限后它起作用了。
回答by Aruna Perera
I know this is a very old thread and the reason may not be the same. But the same issue happened to me.
我知道这是一个非常古老的线程,原因可能不一样。但同样的问题发生在我身上。
The reason was I had an issue inside .env file. This is what I got in .env
原因是我在 .env 文件中有问题。这是我在 .env 中得到的
TEST="e@1asa
Notice that ending quote is missing. And it should be like,
请注意,缺少结束引号。它应该是这样的,
TEST="e@1asa"
All the env variables I added after that returned null due to this error.
由于此错误,我在此之后添加的所有环境变量都返回 null。
Nothing written to error log even. So if you have met this error try adding an example env variable as the first record of the file. If it works and record at last not workingthere may be an error inside .env
甚至没有写入错误日志。因此,如果您遇到此错误,请尝试添加一个示例 env 变量作为文件的第一条记录。如果它有效并且最后记录无效,则 .env 中可能存在错误
回答by Thushara Buddhika
Place .env file at the root folder of the project
将 .env 文件放在项目的根文件夹中
Add these to app.php file.
将这些添加到 app.php 文件中。
try {
(new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
echo $e;
}
回答by Fusion
Looks like there was installed old PHP version on server, which is not situable for Laravel's .env
package to run properly. When I deployed website into a different server with PHP 7 installed, env()
returned values as expected.
看起来服务器上安装了旧的 PHP 版本,这不适合 Laravel 的.env
包正常运行。当我将网站部署到安装了 PHP 7 的不同服务器时,env()
返回的值符合预期。