您好,登錄后才能下訂單哦!
這篇文章主要介紹CI中Setting增強配置類的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體如下:
該增強配置類適用配置項要求比較靈活的項目。可實現預加載配置、組配置、單項調取、增、刪、改配置,無需在改動config文檔。
使用:
在需要的地方
$this->load->library('setting');
對于預加載項可以使用
$this->config->item();
進行獲取
對于臨時調取項可以使用
$this->setting->item();
進行獲取
首先,創建數據表
CREATE TABLE `system_settings` ( `id` int(11) 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=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
然后,在application/libraries目錄下創建setting.php,內容如下
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Setting { private $_ci; private $settings_autoloaded; private $settings = array(); private $settings_group = array(); private $settings_db; public function __construct() { $this->_ci = &get_instance(); $this->settings_db = $this->_ci->config->item('settings_table'); $this->autoload(); } // ------------------------------------------------------------------------ // 華麗的分割線 正式開始 // ------------------------------------------------------------------------ /** * 從數據庫獲取所有自動加載的設置 */ public function autoload() { //如果存在則直接返回 if (!empty($this->settings)) { return $this->settings; } //如果系統不存在數據表則返回false if (!$this->_ci->db->table_exists($this->settings_db)) { return FALSE; } //查詢標記為自動加載的項 $this->_ci->db->select('key,value')->from($this->settings_db)->where('autoload', 'yes'); $query = $this->_ci->db->get(); if ($query->num_rows() == 0) { return FALSE; } //循環寫入系統配置 foreach ($query->result() as $k => $row) { $this->settings[$row->key] = $row->value; $this->_ci->config->set_item($row->key, $row->value); } //標記會話,避免重復讀庫 //$this->_ci->session->set_userdata('settings_autoloaded', TRUE); return $this->settings; } // ------------------------------------------------------------------------ /** * 獲取單個設定 * * <code> * <?php $this->settings->get('config_item'); ?> * </code> */ public function item($key) { if (!$key) { return FALSE; } //首先檢查是否系統已經自動加載 if (isset($this->settings[$key])) { return $this->settings[$key]; } //查詢數據庫 $this->_ci->db->select('value')->from($this->settings_db)->where('key', $key); $query = $this->_ci->db->get(); if ($query->num_rows() > 0) { $row = $query->row(); $this->settings[$key] = $row->value; return $row->value; } // 查詢不到結果則查找系統config,返回值或者false return $this->_ci->config->item($key); } // ------------------------------------------------------------------------ /** * 獲取組配置 */ public function group($group = '') { if (!$group) { return FALSE; } $this->_ci->db->select('key,value')->from($this->settings_db)->where('group', $group); $query = $this->_ci->db->get(); if ($query->num_rows() == 0) { return FALSE; } foreach ($query->result() as $k => $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); $this->_ci->db->update($this->settings_db, array('value' => $value)); if ($this->_ci->db->affected_rows() == 0) { return FALSE; } return TRUE; } // ------------------------------------------------------------------------ /** * 新增設置 */ public function insert($key, $value = '', $group = 'addon', $autoload = 'no') { // 檢查是否已經被添加的設置 $this->_ci->db->select('value')->from($this->settings_db)->where('key', $key); $query = $this->_ci->db->get(); if ($query->num_rows() > 0) { return $this->edit($key, $value); } $data = array('key' => $key, 'value' => $value, 'group' => $group, 'autoload' => $autoload, ); $this->_ci->db->insert($this->settings_db, $data); if ($this->_ci->db->affected_rows() == 0) { return FALSE; } return TRUE; } // ------------------------------------------------------------------------ /** * 刪除設置 */ public function delete($key) { $this->_ci->db->delete($this->settings_db, array('key' => $key)); if ($this->_ci->db->affected_rows() == 0) { return FALSE; } return TRUE; } // ------------------------------------------------------------------------ /** * 刪除設置組及成員配置 */ public function delete_group($group) { $this->_ci->db->delete($this->settings_db, array('group' => $group)); if ($this->_ci->db->affected_rows() == 0) { return FALSE; } return TRUE; } } /* End of file Setting.php */ /* Location: ./application/libraries/Setting.php */
最后,打開application/config/config.php,新增
/** * 系統配置表名 */ $config['settings_table'] = "system_settings";
以上是“CI中Setting增強配置類的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。