Laravel .env 变量总是返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46381917/
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 .env variable always returns null
提问by Erdem
I added this variable to .env file
我将此变量添加到 .env 文件
STRIPE_SECRET=a12345
STRIPE_SECRET=a12345
I would like to dump the variable using routes/web.php
我想使用 routes/web.php 转储变量
<?php
dd(env('STRIPE_SECRET'));
But it looks like it always returns null.
但看起来它总是返回null。
Update : Updated .env file. I only removed DB_PASSWORD.
更新:更新 .env 文件。我只删除了 DB_PASSWORD。
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:08txDXXatyYsP5WQ4ECz35Q7OyBEe8Vgb/zK5fZsHik=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
APP_LOCALE=tr
APP_LC_ALL=tr_TR.utf8
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=gunluk
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=03ac580c85842d
MAIL_PASSWORD=1d6d902d296942
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
STRIPE=a123
STRIPE_SECRET=a12345
回答by Desh901
The main reason upon your issue is that you are caching your configuration. When running php artisan config:cache
you're storing your configuration in your cache, and the next time Laravel will boot up it won't read the .env
file because it detects that the configuration has been stored in the cache. Environment file should be used only to setup configuration files and then to access the value you're looking for you should use ONLY the config
method.
您的问题的主要原因是您正在缓存您的配置。运行时,php artisan config:cache
您将配置存储在缓存中,下次 Laravel 启动时,它不会读取.env
文件,因为它检测到配置已存储在缓存中。环境文件应该仅用于设置配置文件,然后访问您正在寻找的值,您应该仅使用该config
方法。
Let's assume that you have the file config/stripe.php
that consists of this content:
假设您有config/stripe.php
包含以下内容的文件:
<?php
return [
'secret' => env('STRIPE_SECRET', '')
];
Once you run php artisan config:cache
access this value using ONLY the syntax config('stripe.secret')
through your application code. Every time you update your config files and your .env
you need to run php artisan config:cache
again.
运行后,php artisan config:cache
仅使用语法config('stripe.secret')
通过应用程序代码访问此值。每次更新配置文件时,您.env
都需要php artisan config:cache
再次运行。
回答by RAUSHAN KUMAR
First, there is no STRIPE_SECRET
inside your .env
file(As per before edit the question). So please make sure that your .env
must have this variable. You should clear your configuration cache by executing following commands in the same order
首先,STRIPE_SECRET
您的.env
文件中没有(根据编辑问题之前的内容)。所以请确保你.env
必须有这个变量。您应该通过按相同顺序执行以下命令来清除配置缓存
php artisan config:cache
php artisan config:clear
Laravel cache your configuration files so the execution become faster. So everytime when you change the configuration files on server, you should clear the cache.
Laravel 缓存您的配置文件,因此执行速度更快。因此,每次更改服务器上的配置文件时,都应清除缓存。
Additionally you can run these commands also to clear the other caches
此外,您还可以运行这些命令来清除其他缓存
php artisan cache:clear //for clearing the cache
php artisan view:clear //for clearing the compiled views
php artisan route:clear //for clearing the routes cache
You can also create the routes for these commands and call the commands from the code also as
您还可以为这些命令创建路由,并从代码中调用命令,也可以作为
Route::get('/cache-clear', function() {
$exitCode = Artisan::call('cache:clear');
echo "Cache Cleard: ".$exitCode;
});
Route::get('/view-clear', function() {
$exitCode = Artisan::call('view:clear');
echo "View Cleard: ".$exitCode;
});
Route::get('/route-cache', function() {
$exitCode = Artisan::call('route:cache');
echo "Route Cached: ".$exitCode;
});
Route::get('/route-clear', function() {
$exitCode = Artisan::call('route:clear');
echo "Route Cache Cleared: ".$exitCode;
});
Route::get('/config-cache', function() {
$exitCode = Artisan::call('config:cache');
echo "Config Cached: ".$exitCode;
});
Route::get('/config-clear', function() {
$exitCode = Artisan::call('config:clear');
echo "Config Cache Cleared: ".$exitCode;
});
回答by Bilal Ahmed
if you have
STRIPE="a12345"
this in .env
file or if you any change in .env
file or config
file then you fallow these steps
如果您STRIPE="a12345"
在.env
文件中有
此内容,或者您在.env
文件或config
文件中有任何更改,那么您可以使用这些步骤
one more thing write the variable value in comma's like STRIPE="a12345"
还有一件事用逗号写变量值 STRIPE="a12345"
First run these commands
首先运行这些命令
1. php artisan config:clear
2. php artisan cache:clear
3. composer dump-autoload
and finally used this command to get variable
最后用这个命令来获取变量
dd(env('STRIPE'));
this working for me
这对我有用
and also 1 stupid suggestion: restart server
还有 1 个愚蠢的建议:重启服务器
I have add all possible solution
我已经添加了所有可能的解决方案
回答by iCoders
You can clear configuration cache using following commands
您可以使用以下命令清除配置缓存
php artisan config:clear
php artisan optimize
php artisan config:cache
Also make sure
还要确保
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.
如果您config:cache
在部署期间使用该命令,则必须确保仅从配置文件中调用 env 函数,而不是从应用程序的任何其他地方调用。如果您从应用程序中调用 env,强烈建议您向配置文件添加适当的配置值,然后从该位置调用 env,从而允许您将 env 调用转换为配置调用。
Reference link:
参考链接:
Update 1:
更新 1:
Also make sure you have renamed .env.example
to .env file
by default laravel have .env.example
还要确保您已重命名.env.example
为.env file
默认情况下 laravel 有.env.example
Update 2
更新 2
As per your new update question your env file have STRIPE
not STRIPE_SECRET
so you have access like this if its not typo error in question
根据您的新更新问题,您的 env 文件STRIPE
没有,STRIPE_SECRET
因此如果不是有问题的拼写错误,您可以像这样访问
env('STRIPE')
回答by Moeen Basra
make sure you app is completely booted.
确保您的应用程序已完全启动。
if you are running server with cmd then try restarting server.
如果您使用 cmd 运行服务器,请尝试重新启动服务器。
and if it not works try clearing the cache using the methods above mentioned by @iCoders.
如果它不起作用,请尝试使用@iCoders 上面提到的方法清除缓存。
回答by Yevgeniy Afanasyev
env(...)
function will return null after you cached the config. (starting from laravel 5.2 till current 5.7)
env(...)
缓存配置后,函数将返回 null。(从 laravel 5.2 到当前的 5.7)
The 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工匠配置:清除