4

您可能知道,当您使用 CI 创建一个新项目时,您必须在 config/config.php 中手动输入基本 url、加密密钥。我正在努力克服这个问题,因此正在寻找一种从数据库中读取这些值的方法——使客户的安装和整个设置时间减少了很多。

客户无法编辑 PHP 文件的变量,但很可能在一些指导下能够输入基本 url 并让系统自动填写加密密钥。

有没有办法做到这一点?

4

3 回答 3

5

当然!添加一个钩子 - 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);

    }

}

基本上,您在将它们用于任何控制器或类似设备之前设置这些值。

于 2012-05-20T14:53:04.723 回答
3

至于Codeigniter 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);

    }

}

现在它将起作用!

于 2017-08-04T18:11:47.973 回答
0
 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;     
于 2017-10-05T09:37:50.497 回答