将数据库值设置为配置项编码器


Set Database Value As Config Item Codeigniter

我如何能够获得键值并将其设置为config_item。我一直试图能够得到,所以可以通过表格和数据库做到这一点。我只能手动修改配置项。

我有一个表单,它找到主题,然后保存我选择的数据库。

Table Name Setting
setting_id = 1
group = config 
key = config_template
value = Name of theme submitted in form.

我希望能够以某种方式使用$this->config->item('config_template'),以便它将获得值。

因为我想实现这个前端控制器

 <?php
      class Home extends CI_Controller {
        public function index() {
          if(file_exists(DIR_TEMPLATE . $this->config->item('config_template') . '/template/common/home.php')) {
             $this->load->view($this->config->item('config_template') . '/template/common/home');
          } else {
             $this->load->view('default/template/common/home');
          }
        }
      }

后端设置控制器

<?php
class Setting extends CI_Controller {
    public function index() {
        $this->load->model('setting_model');
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $this->setting_model->editSetting('config', $this->input->post());
            redirect('store');
        }
        $data['action'] = site_url('setting');
        if (null !==($this->input->post('config_template'))) {
            $data['config_template'] = $this->input->post('config_template');
        } else {
            $data['config_template'] = $this->config->item('config_template');
        }
        $data['templates'] = array();
        $directories = glob(DIR_APPLICATION . 'views/theme/*', GLOB_ONLYDIR);
        foreach ($directories as $directory) {
            $data['templates'][] = basename($directory);
        }
        return $this->load->view('setting/setting', $data);
    }
 }

你必须在你的模型/控制器中写一个钩子来从数据库中获取值,并在运行时设置它,也许在__construct()

中设置它们
function __construct() {
    parent::__construct();
    //get $value from database here
    $this->config->set_item('config_template', $value);
}

然后在剩下的代码中,您可以使用

$this->config->item('config_template')

检索值