CI(Codeigniter)的Setting增強(qiáng)配置類實(shí)例
本文實(shí)例講述了Codeigniter的Setting增強(qiáng)配置類。分享給大家供大家參考,具體如下:
該增強(qiáng)配置類適用配置項(xiàng)要求比較靈活的項(xiàng)目??蓪?shí)現(xiàn)預(yù)加載配置、組配置、單項(xiàng)調(diào)取、增、刪、改配置,無需在改動config文檔。
使用:
在需要的地方
對于預(yù)加載項(xiàng)可以使用
對于臨時(shí)調(diào)取項(xiàng)可以使用
首先,創(chuàng)建數(shù)據(jù)表
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目錄下創(chuàng)建setting.php,內(nèi)容如下
<?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();
}
// ------------------------------------------------------------------------
// 華麗的分割線 正式開始
// ------------------------------------------------------------------------
/**
* 從數(shù)據(jù)庫獲取所有自動加載的設(shè)置
*/
public function autoload() {
//如果存在則直接返回
if (!empty($this->settings)) {
return $this->settings;
}
//如果系統(tǒng)不存在數(shù)據(jù)表則返回false
if (!$this->_ci->db->table_exists($this->settings_db)) {
return FALSE;
}
//查詢標(biāo)記為自動加載的項(xiàng)
$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;
}
//循環(huán)寫入系統(tǒng)配置
foreach ($query->result() as $k => $row) {
$this->settings[$row->key] = $row->value;
$this->_ci->config->set_item($row->key, $row->value);
}
//標(biāo)記會話,避免重復(fù)讀庫
//$this->_ci->session->set_userdata('settings_autoloaded', TRUE);
return $this->settings;
}
// ------------------------------------------------------------------------
/**
* 獲取單個(gè)設(shè)定
*
* <code>
* <?php $this->settings->get('config_item');
?>
* </code>
*/
public function item($key) {
if (!$key) {
return FALSE;
}
//首先檢查是否系統(tǒng)已經(jīng)自動加載
if (isset($this->settings[$key])) {
return $this->settings[$key];
}
//查詢數(shù)據(jù)庫
$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;
}
// 查詢不到結(jié)果則查找系統(tǒng)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;
}
// ------------------------------------------------------------------------
/**
* 更改設(shè)置
*/
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;
}
// ------------------------------------------------------------------------
/**
* 新增設(shè)置
*/
public function insert($key, $value = '', $group = 'addon', $autoload = 'no') {
// 檢查是否已經(jīng)被添加的設(shè)置
$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;
}
// ------------------------------------------------------------------------
/**
* 刪除設(shè)置
*/
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;
}
// ------------------------------------------------------------------------
/**
* 刪除設(shè)置組及成員配置
*/
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,新增
/** * 系統(tǒng)配置表名 */ $config['settings_table'] = "system_settings";
希望本文所述對大家基于Codeigniter框架的PHP程序設(shè)計(jì)有所幫助。
- CI(CodeIgniter)模型用法實(shí)例分析
- CodeIgniter輔助之第三方類庫third_party用法分析
- CodeIgniter分頁類pagination使用方法示例
- CodeIgniter圖像處理類的深入解析
- codeigniter中測試通過的分頁類示例
- 使用CodeIgniter的類庫做圖片上傳
- Codeigniter整合Tank Auth權(quán)限類庫詳解
- CodeIgniter基于Email類發(fā)郵件的方法
- CodeIgniter擴(kuò)展核心類實(shí)例詳解
- php實(shí)現(xiàn)仿寫CodeIgniter的購物車類
- Codeigniter的dom類用法實(shí)例
- CI框架(CodeIgniter)公共模型類定義與用法示例
相關(guān)文章
php實(shí)現(xiàn)文本數(shù)據(jù)導(dǎo)入SQL SERVER
php將文本文件導(dǎo)入mysql我們經(jīng)常遇到,但是如果是導(dǎo)入到sqlserver又應(yīng)該如何操作呢,下面就給大家分享一下本人的操作方法,感覺效率還不錯,這里推薦給大家。2015-05-05
md5 16位二進(jìn)制與32位字符串相互轉(zhuǎn)換示例
密碼很多時(shí)候都會用 md5保存,并且很多時(shí)候都是16位二進(jìn)制格式的md5,php 里面 md5($str, true) 可以很方便的獲取。更多時(shí)候md5結(jié)果是一組32個(gè)字符組成的字符串,其實(shí)轉(zhuǎn)換很簡單2013-12-12
Thinkphp 5.0實(shí)現(xiàn)微信企業(yè)付款到零錢
這篇文章主要為大家詳細(xì)介紹了Thinkphp 5.0實(shí)現(xiàn)微信企業(yè)付款到零錢,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
實(shí)例講解yii2.0在php命令行中運(yùn)行的步驟
Yii中的資源是和Web頁面相關(guān)的文件,可為CSS文件,JavaScript文件,圖片或視頻等,資源放在Web可訪問的目錄下,直接被Web服務(wù)器調(diào)用。本文通過實(shí)例講解yii2.0在php命令行中運(yùn)行的步驟,對yii2.0 php相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2015-12-12
基于php和mysql的簡單的dao類實(shí)現(xiàn)crud操作功能
一個(gè)簡單的dao,實(shí)現(xiàn)基本的CRUD功能,可以繼承擴(kuò)展為實(shí)際業(yè)務(wù)的dao類,當(dāng)然也可以直接使用2014-01-01
php實(shí)現(xiàn)當(dāng)前頁面點(diǎn)擊下載文件的簡單方法
下面小編就為大家?guī)硪黄猵hp實(shí)現(xiàn)當(dāng)前頁面點(diǎn)擊下載文件的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
php靜態(tài)成員方法和靜態(tài)的成員屬性的使用方法
這篇文章主要介紹了php靜態(tài)成員方法和靜態(tài)的成員屬性的使用方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家使用的時(shí)候注意方法,需要的朋友可以參考下2017-10-10
Laravel實(shí)現(xiàn)構(gòu)造函數(shù)自動依賴注入的方法
這篇文章主要介紹了Laravel實(shí)現(xiàn)構(gòu)造函數(shù)自動依賴注入的方法,涉及Laravel構(gòu)造函數(shù)自動初始化的相關(guān)技巧,需要的朋友可以參考下2016-03-03

