通过Laravel中的控制器更改环境值 [英] Changing env values via controller in laravel
问题描述
是否可以通过laravel中的控制器更改 .env
文件的值?
Is there any ways that I could change my .env
file values from controllers in laravel?
我找到了此答案,但它返回了>
I've found this answer but it returns
未定义的属性:App \ Http \ Controllers \ Admin \ PerformanceController :: $ laravel
Undefined property: App\Http\Controllers\Admin\PerformanceController::$laravel
代码
$path = base_path('.env');
$key = false;
if (file_exists($path)) {
file_put_contents($path, str_replace(
'APP_KEY='.$this->laravel['config']['app.key'], 'APP_DEBUG='.$key, file_get_contents($path)
));
}
我想在管理面板中使用选项在true或false之间更改调试模式,就像我们在控制器中使用artisan命令一样,例如 Artisan :: call('down')
或Artisan :: call('up')
之类的东西.
I want to have options in my admin panel to change debug mode between true or false, same as we have artisan commands in controller like Artisan::call('down')
or Artisan::call('up')
something like that.
现在我有了这段代码
$path = base_path('.env');
$key = 'true';
if (file_exists($path)) {
file_put_contents($path, str_replace(
'APP_DEBUG='.config('app.debug'), 'APP_DEBUG='.$key, file_get_contents($path)
));
}
此代码确实有效,但问题在于它不会删除旧值.
this code does work but the issue is that it doesn't remove old value.
APP_DEBUG=false
之后
APP_DEBUG=truefalse
or
APP_DEBUG=falsefalse
有什么主意吗?
推荐答案
使用此DotenvEditor :: setKey('APP_KEY','new_value')-> save();
use this DotenvEditor::setKey('APP_KEY', 'new_value')->save();
这篇关于通过Laravel中的控制器更改环境值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!