2017-01-27 22 views
2

我在Laravel 5.3中創建了一個mailable類,並在config/mail.php中設置了名稱和返回電子郵件的全局變量。我希望這些值可以由管理員用戶編輯,而不需要他們去配置文件或編輯.env。我該怎麼做呢?用戶如何在Laravel中編輯配置值?

+0

你可以把config的默認值作爲第二個參數。我不認爲你可以編輯它 – Vikash

+0

你需要改變它們每個執行或永久飛行? –

+0

@AvikAghajanyan理想情況下,它將是半永久性的,以便管理員可以更改它,並將其應用於所有電子郵件,直到它再次更改。 – Birdman

回答

0

中有Laravel沒有這樣的功能,所以你需要使用的軟件包,以便之一,例如this one

0

你可以寫這樣的輔助函數來編輯.ENV

function saveConfiguration($values, $configFilename = '.env') { 
     if (empty($values) || !is_array($values)) { 
      return false; 
     } 

     $envFile = base_path($configFilename); 
     if (!File::exists($envFile)) { 
      $existingConfig = []; 
     } else { 
      $existingConfig = file($envFile); 
     } 

     $configs = []; 
     foreach ($existingConfig as $config) { 
      if (!empty(str_replace(' ', '', $config))) { 
       $config = str_replace([ 
        "\r", 
        "\n" 
       ], ['', ''], $config); 
       $configParts = explode('=', $config, 2); 
       if (!empty($configParts[1])) { 
        if (!array_key_exists($configParts[0], $values)) { 
         $configs[] = $configParts[0].'='.$configParts[1]; 
        } 
       } 
      } 
     } 

     foreach ($values as $key => $value) { 
      $value = str_replace('"', '\"', $value); 
      if (strpos($values[$key], ' ') !== false) { 
       $configs[] = $key.'="'.$value.'"'; 
      } else { 
       $configs[] = $key.'='.$value; 
      } 
     } 

     File::put($envFile, implode("\n", $configs)); 

     return true; 
    } 

然後使用它保存在.env中,並配置你的config/mail.php使用那裏的憑證