从数据库中读取配置笨值而不是配置文件的(Reading CodeIgniter config val

2019-07-29 11:58发布

正如你可能知道,当你创建一个新的项目,CI你必须手动输入基本URL,在配置/ config.php文件加密密钥 。 我想克服这个,并因此寻找一种方法来从数据库中 读取 ,而不是那些价值-使得安装的客户和建立时间整体下降了很多。

一位顾客不能编辑PHP文件的变量,但最有可能能,具有一定的指导,输入基本URL,并有由系统自动填充的一个加密密钥。

有没有办法做到这一点?

Answer 1:

当然! 添加钩- post_controller并通过该文件可以设置这些配置值。

config/hooks.php

$hook['pre_controller'][] = array(  'class'    => 'MyOtherClass',
                                    'function' => 'MyOtherfunction',
                                    'filename' => 'Myotherclass.php',
                                    'filepath' => 'hooks');

hooks/myotherclass.php

<?

class MyOtherClass {

    function MyOtherfunction() {

        $CI =& get_instance();

        $row = $CI->db->get_where('configs', array('site_id' => 1))->row();

        $CI->config->set_item('base_url', $row->base_url);

    }

}

Basicly你他们在任何控制器或similiar使用前设置这些值。



Answer 2:

据笨3.1.7 pre_controller不会工作,因为在初始化它会返回NULL$CI =& get_instance();

为了解决这个问题:

改变从挂钩点pre_controllerpost_controller_constructor

修改后的源:

config/hooks.php

$hook['post_controller_constructor'][] = array(  'class'    => 'MyOtherClass',
                                'function' => 'MyOtherfunction',
                                'filename' => 'Myotherclass.php',
                                'filepath' => 'hooks');

并在hooks/myotherclass.php

<?

class MyOtherClass {

    function MyOtherfunction() {

        $CI =& get_instance();

        $row = $CI->db->get_where('configs', array('site_id' => 1))->row();

        $CI->config->set_item('base_url', $row->base_url);

    }

}

现在,它的工作!



Answer 3:

 class name extends CI_Controller {
    public $DB1;
    public $MSSQL;
    public function __construct()
     {
    parent::__construct();
        $userid = $this->session->userdata('userids');
          $this->load->model('kubix');
          $result = $this->kubix->databaseconncetion($userid);
        foreach ($result as $deta)
          {

      $this->MSSQL=$db['MSSQL'] = array(
      'hostname' => $deta["Db_base_ip"],
      'username' => $deta["Db_username"],
      'password' => $deta["db_password"],
      'database' => $deta["Db_base_name"],
      'dbdriver' => 'sqlsrv',
      'dbprefix' => '',
      'pconnect' => FALSE,
      'db_debug' => (ENVIRONMENT !== 'production'),
      'cache_on' => FALSE,
      'cachedir' => '',
      'char_set' => 'utf8',
      'dbcollat' => 'utf8_general_ci',
      'swap_pre' => '',
      'encrypt' => FALSE,
      'compress' => FALSE,
      'stricton' => FALSE,
      'failover' => array(),
      'save_queries' => TRUE);
      $this->DB1 = $this->load->database($this->MSSQL, TRUE);
    $sql = "SELECT  TOP 1 * FROM AccountsFinancialYearMaster where Active=1 ";
        $query = $this->DB1->query( $sql);
        $result= $query->result_array();
RETURN $result;     


文章来源: Reading CodeIgniter config values from database instead of config file