


CI (Codeigniter) Setting enhanced configuration class example, cicodeigniter_PHP tutorial
CI (Codeigniter)'s Setting enhanced configuration class example, cicodeigniter
This article describes the Codeigniter's Setting enhanced configuration class example. Share it with everyone for your reference, the details are as follows:
This enhanced configuration class is suitable for projects that require more flexible configuration items. It can realize preloading configuration, group configuration, single item retrieval, addition, deletion and modification of configuration without changing the config document.
Use:
Where needed
Copy code The code is as follows: $this->load->library('setting');
For preloaded items, you can use to copy the code The code is as follows: $this->config->item(); to obtain
For temporary retrieval items, you can use to copy the code The code is as follows: $this->setting->item(); to obtain
First, create the data table
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;
Then, create setting.php in the application/libraries directory with the following content
<?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 */
Finally, open application/config/config.php and add
/** * 系统配置表名 */ $config['settings_table'] = "system_settings";
I hope this article will be helpful to everyone’s PHP program design based on the Codeigniter framework.
Articles you may be interested in:
- Using Smarty3 basic configuration in CodeIgniter
- Configuring codeigniter framework method under Nginx
- nginx supports codeigniter’s pathinfo mode url Example of rewriting configuration writing method
- Database configuration using codeigniter under Sina SAE cloud platform
- Use configuration class to define Codeigniter global variables
- CI (CodeIgniter) framework configuration
- Detailed introduction to the basic configuration of CodeIgniter
- Parsing CodeIgniter custom configuration file

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Title: How to correctly configure Git in PyCharm In modern software development, the version control system is a very important tool, and Git, as one of the popular version control systems, provides developers with powerful functions and flexible operations. As a powerful Python integrated development environment, PyCharm comes with support for Git, allowing developers to manage code versions more conveniently. This article will introduce how to correctly configure Git in PyCharm to facilitate better development during the development process.

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

PyCharm is a powerful integrated development environment (IDE), and PyTorch is a popular open source framework in the field of deep learning. In the field of machine learning and deep learning, using PyCharm and PyTorch for development can greatly improve development efficiency and code quality. This article will introduce in detail how to install and configure PyTorch in PyCharm, and attach specific code examples to help readers better utilize the powerful functions of these two. Step 1: Install PyCharm and Python

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

How to configure a workgroup in Win11 A workgroup is a way to connect multiple computers in a local area network, which allows files, printers, and other resources to be shared between computers. In Win11 system, configuring a workgroup is very simple, just follow the steps below. Step 1: Open the "Settings" application. First, click the "Start" button of the Win11 system, and then select the "Settings" application in the pop-up menu. You can also use the shortcut "Win+I" to open "Settings". Step 2: Select "System" In the Settings app, you will see multiple options. Please click the "System" option to enter the system settings page. Step 3: Select "About" In the "System" settings page, you will see multiple sub-options. Please click

PyCharm is a commonly used integrated development environment (IDE). In daily development, using Git to manage code is essential. This article will introduce how to configure Git in PyCharm and use Git for code management, with specific code examples. Step 1: Install Git First, make sure Git is installed on your computer. If it is not installed, you can go to [Git official website](https://git-scm.com/) to download and install the latest version of Git

Title: How to configure and install FTPS in Linux system, specific code examples are required. In Linux system, FTPS is a secure file transfer protocol. Compared with FTP, FTPS encrypts the transmitted data through TLS/SSL protocol, which improves Security of data transmission. In this article, we will introduce how to configure and install FTPS in a Linux system and provide specific code examples. Step 1: Install vsftpd Open the terminal and enter the following command to install vsftpd: sudo

DRBD (DistributedReplicatedBlockDevice) is an open source solution for achieving data redundancy and high availability. Here is the tutorial to install and configure DRBD on CentOS7 system: Install DRBD: Open a terminal and log in to the CentOS7 system as administrator. Run the following command to install the DRBD package: sudoyuminstalldrbd Configure DRBD: Edit the DRBD configuration file (usually located in the /etc/drbd.d directory) to configure the settings for DRBD resources. For example, you can define the IP addresses, ports, and devices of the primary node and backup node. Make sure there is a network connection between the primary node and the backup node.
