Skip to content

Instantly share code, notes, and snippets.

@mustafat0k
Last active March 9, 2019 16:03
Show Gist options
  • Save mustafat0k/91826942ffb780c641d4f3dd8da1432b to your computer and use it in GitHub Desktop.
Save mustafat0k/91826942ffb780c641d4f3dd8da1432b to your computer and use it in GitHub Desktop.
Env kaydet
<?php
/** Mail Ayarları */
public function getSmtp() {
$smtp=Config::get('mail');
return view('backend.setting.smtp',compact('smtp'));
}
public function postSmtp(Request $request) {
$env_update = $this->changeEnv([
'MAIL_HOST'=>$request->mail_host,
'MAIL_PORT'=>$request->mail_port,
'MAIL_USERNAME'=>$request->mail_user,
'MAIL_PASSWORD'=>$request->mail_pass,
'MAIL_ENCRYPTION'=>$request->mail_enc,
'MAIL_FROM_ADDRESS'=>$request->mail_from_adres,
'MAIL_FROM_NAME'=>$request->mail_from_user,
]);
if($env_update){
return redirect(route('setting.smtp'))->with(['success'=>'SMTP bilgileri başarıyla güncellendi.']);
} else {
return redirect(route('setting.smtp'))->with(['error'=>'SMTP bilgileri güncellenemedi.']);
}
}
protected function changeEnv($data = array()){
if(count($data) > 0){
// Read .env-file
$env = file_get_contents(base_path() . '/.env');
// Split string on every " " and write into array
$env = explode("\n", $env);
// Loop through given data
foreach((array)$data as $key => $value){
// Loop through .env-data
foreach($env as $env_key => $env_value){
// Turn the value into an array and stop after the first split
// So it's not possible to split e.g. the App-Key by accident
$entry = explode("=", $env_value, 2);
// Check, if new key fits the actual .env-key
if($entry[0] == $key){
// If yes, overwrite it with the new one
$env[$env_key] = $key . '="'.$value.'"';
} else {
// If not, keep the old one
$env[$env_key] = $env_value;
}
}
}
// Turn the array back to an String
$env = implode("\n", $env);
// And overwrite the .env with the new data
file_put_contents(base_path() . '/.env', $env);
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment