CI中Setting增强配置类的示例分析
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,这篇文章主要介绍CI中Setting增强配置类的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体如下:该增强配置类适用配置项要求比较灵活的项目。可实现预加载配置、组配
千家信息网最后更新 2025年02月04日CI中Setting增强配置类的示例分析
这篇文章主要介绍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,内容如下
_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; } // ------------------------------------------------------------------------ /** * 获取单个设定 * * * settings->get('config_item'); ?> *
*/ 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增强配置类的示例分析"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
配置
系统
数据
内容
查询
示例
分析
数据库
数据表
标记
篇文章
检查
价值
兴趣
分割线
单个
单项
地方
小伙
小伙伴
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
农行山西软件开发中心
数据库连接字符串要素
数据库的封锁机制
服务器连接线
极进网络技术
二层网络技术说明
42u标准服务器机柜尺寸
学校网络安全宣传工作方案
如何做数据库管理公司信息
2_什么是网络安全体系
电商系统数据库设计图
手机上怎么样学习网络技术
网络安全扫描能
国家网络安全应急措施
锐捷网络安全插画
2网络安全宣传周简报
医疗软件开发的前景
js获得复选框的数据库
服务器常见的安全风险有哪些
TC中显示数据库没有会话记录
服务器如何链接软件
沈阳直播软件开发公司如何选择
山东高校网络安全研究生
手机网络安全已成为
数据库运用技术
单核服务器承载量
西安铁程网络技术有限公司地址
数据库显示一个表
房山区数据网络技术售后服务
服务器配件价格