解決 CodeIgniter 原生 config.php 靜態、難以動態修改、無分組機制的問題。將配置項存入資料庫,支援自動載入、分組隔離、單項讀寫、完整 CRUD,無需修改配置檔案即可熱更新。
CREATE TABLE `system_settings` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`key` VARCHAR(64) NOT NULL DEFAULT '',
`value` MEDIUMTEXT NOT NULL,
`group` VARCHAR(55) NOT NULL DEFAULT 'site',
`autoload` ENUM('no','yes') NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`,`key`),
KEY `name` (`key`),
KEY `autoload` (`autoload`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
_ci =& get_instance();
$this->settings_db = $this->_ci->config->item('settings_table');
$this->autoload();
}
/** 自動載入標記為 yes 的配置到 CI config */
public function autoload() {
if (!empty($this->settings)) return $this->settings;
if (!$this->_ci->db->table_exists($this->settings_db)) return FALSE;
$query = $this->_ci->db->select('key,value')
->from($this->settings_db)
->where('autoload', 'yes')
->get();
if ($query->num_rows() === 0) return FALSE;
foreach ($query->result() as $row) {
$this->settings[$row->key] = $row->value;
$this->_ci->config->set_item($row->key, $row->value);
}
return $this->settings;
}
/** 取得單項配置 (優先快取 -> DB -> CI config) */
public function item($key) {
if (!$key) return FALSE;
if (isset($this->settings[$key])) return $this->settings[$key];
$query = $this->_ci->db->select('value')
->from($this->settings_db)
->where('key', $key)
->get();
if ($query->num_rows() > 0) {
$row = $query->row();
$this->settings[$key] = $row->value;
return $row->value;
}
return $this->_ci->config->item($key);
}
/** 取得整組配置 */
public function group($group = '') {
if (!$group) return FALSE;
$query = $this->_ci->db->select('key,value')
->from($this->settings_db)
->where('group', $group)
->get();
if ($query->num_rows() === 0) return FALSE;
$arr = [];
foreach ($query->result() as $row) {
$this->settings[$row->key] = $row->value;
$arr[$row->key] = $row->value;
}
return $arr;
}
/** 修改配置 */
public function edit($key, $value) {
$this->_ci->db->where('key', $key)
->update($this->settings_db, ['value' => $value]);
return $this->_ci->db->affected_rows() > 0;
}
/** 新增配置 (存在則更新) */
public function insert($key, $value = '', $group = 'addon', $autoload = 'no') {
$query = $this->_ci->db->select('value')
->from($this->settings_db)
->where('key', $key)
->get();
if ($query->num_rows() > 0) return $this->edit($key, $value);
$data = ['key'=>$key, 'value'=>$value, 'group'=>$group, 'autoload'=>$autoload];
$this->_ci->db->insert($this->settings_db, $data);
return $this->_ci->db->affected_rows() > 0;
}
/** 刪除單項 */
public function delete($key) {
$this->_ci->db->delete($this->settings_db, ['key' => $key]);
return $this->_ci->db->affected_rows() > 0;
}
/** 刪除整組 */
public function delete_group($group) {
$this->_ci->db->delete($this->settings_db, ['group' => $group]);
return $this->_ci->db->affected_rows() > 0;
}
}
// application/config/config.php $config['settings_table'] = 'system_settings';
// 載入 Library
$this->load->library('setting');
// 讀取自動載入項 (同原生 config)
$siteName = $this->config->item('site_name');
// 讀取動態項
$apiKey = $this->setting->item('api_key');
// 讀取整組
$mailConf = $this->setting->group('mail'); // ['host'=>'...', 'port'=>587, ...]
// 寫入/更新
$this->setting->insert('maintenance_mode', '1', 'system', 'yes');
$this->setting->edit('site_name', '新站名');
// 刪除
$this->setting->delete('old_key');
$this->setting->delete_group('deprecated_module');
autoload() 結果寫入 Redis/Memcached,item() 先查快取,減少 DB 查詢。json_decode,支援複雜型別。edit/insert/delete 後觸發 Event,清除快取、通知其他 Worker 重載。version、updated_by 欄位,支援配置回滾。// CI4 使用 Config 類別 + 資料庫驅動
namespace App\Config;
use CodeIgniter\Config\BaseConfig;
class Settings extends BaseConfig {
public $settings_table = 'system_settings';
// 可繼承 BaseConfig 的 get()/set() 自動支援 .env 覆寫
}
Setting Library 將 CodeIgniter 配置管理從靜態檔案升級為資料庫驅動、動態熱更新、分組隔離的現代化機制。核心優勢:autoload 無縫兼容原生 $this->config->item(),group() 批次讀取模組配置,完整 CRUD 支援運行期配置變更。生產環境建議加入快取層與變更事件,確保高併發下配置讀取效能與一致性。