通过 Laravel 更新 .env 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42727510/
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
Update .env value via Laravel
提问by cyber8200
Example, if my .envis
例如,如果我的.env是
APP_ENV=local
APP_URL=http://localhost:8888/
APP_DEBUG=true
FACEBOOK_APP_ID = ABC
I know in Laravel we can do access our .env file by doing this
我知道在 Laravel 中我们可以通过这样做来访问我们的 .env 文件
echo env('APP_ENV'); --> 'local'
echo env('APP_URL'); --> 'http://localhost:8888/'
but I wonder if there is a way to programmatically set it
但我想知道是否有办法以编程方式设置它
Ex. env('APP_ENV') == 'production';
采纳答案by ATechGuy
try this
尝试这个
$path = base_path('.env');
if (file_exists($path)) {
file_put_contents($path, str_replace(
'APP_KEY='.$this->laravel['config']['app.key'], 'APP_KEY='.$key, file_get_contents($path)
));
}
taken from here stack answer
取自这里堆栈答案
回答by Saiful Islam
This might help.
这可能会有所帮助。
function setEnv($name, $value)
{
$path = base_path('.env');
if (file_exists($path)) {
file_put_contents($path, str_replace(
$name . '=' . env($name), $name . '=' . $value, file_get_contents($path)
));
}
}
setEnv('APP_ENV','abc');
回答by apokryfos
You can use:
您可以使用:
putenv("APP_ENV=production");
because env
is just a convenience wrapper around getenv
which does some type conversion.
因为env
只是一个方便的包装器getenv
,可以进行一些类型转换。
Note: this is not permanent as it will be overwritten by .env
when the next request happens.
注意:这不是永久性的,因为它会.env
在下一个请求发生时被覆盖。