ThinkPHP5.0.1版本開始增加了擴展配置目錄的概念,在應用配置目錄或者模塊配置目錄下面增加extra子目錄,下面的配置文件都會自動加載,無需任何配置。
這極大的方便了我們進行擴展配置,比如在application/extra目錄下面創建一個web.php文件,內容如下:

這樣我們很就可以方便的獲取這些擴展配置信息。
在控制器中獲取網站名稱:
Config::get('web.WEB_NAME');
在模板中獲取網站備案號:
{$Think.config.third.WEB_BEIAN}
我們還可以創建一個專門的控制器來管理這個配置文件,示例具體如下:
function add() { if (request()->isPost()) { $path = 'extra/web.php'; $file = (include $path); $config = array( 'WEB_COPYRIGHT' => input('WEB_COPYRIGHT') ); $res = array_merge($file, $config); $str = '<?php return ['; foreach ($res as $key => $value) { $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ','; } $str .= ']; '; if (file_put_contents($path, $str)) { $this->success('添加成功'); } else { $this->error('添加失敗'); } } }
修改的話思路相同,先讀取所有配置,然后循環輸出到表單,在根據提交循環寫入更新。
來源:http://www.02405.com/program/php/1755.html
