laravel 动态修改.env文件实现配置项改变
public function language(){ $data=['lang'=>request('lang')]; $envPath = base_path() . DIRECTORY_SEPARATOR . '.env'; $contentArray = collect(file($envPath, FILE_IGNORE_NEW_LINES)); $contentArray->transform(function ($item) use ($data){ foreach ($data as $key => $value){ if(str_contains($item, $key)){ return $key . '=' . $value; } } return $item; }); $content = implode($contentArray->toArray(), "\n"); \File::put($envPath, $content); return redirect()->back(); }
需求是多语言切换,当点击切换语言时候改变.env文件来实现语言的切换