CICodeigniter的Setting增強配置類執行個體

來源:互聯網
上載者:User
本文執行個體講述了Codeigniter的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,內容如下

<?phpif (!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;  }  // ------------------------------------------------------------------------  /**   * 擷取單個設定   *   *    * <?php $this->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";

希望本文所述對大家基於Codeigniter架構的PHP程式設計有所協助。

以上就介紹了CICodeigniter的Setting增強配置類執行個體,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.