codeigniter加载外部配置文件

codeigniter加载外部配置文件,codeigniter,config,loader,Codeigniter,Config,Loader,我正在使用CodeIgniter,我想加载位于外部文件夹中的一个或多个配置文件,由不同的CI安装共享。 可能吗 我尝试扩展Loader类,并调用新方法: $this->load->external_config('MY\external\PATH') 加载成功,但我无法在控制器中检索配置项,因为在我的\u Loader类中,core\Loader config属性不可见,并且我无法将其与新加载的值合并 这是我的代码: <?php if ( ! defined('BASEPATH')) ex

我正在使用CodeIgniter,我想加载位于外部文件夹中的一个或多个配置文件,由不同的CI安装共享。 可能吗

我尝试扩展Loader类,并调用新方法: $this->load->external_config('MY\external\PATH')

加载成功,但我无法在控制器中检索配置项,因为在我的\u Loader类中,core\Loader config属性不可见,并且我无法将其与新加载的值合并

这是我的代码:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Loader extends CI_Loader {

 /**
  * List of all loaded config files
  *
  * @var array
  */
 var $is_config_loaded = array();

 /**
  * List of all loaded config values
  *
  * @var array
  */
 //var $ext_config = array();

 function __construct(){
        parent::__construct();
    }


    /**
  * Loads an external config file
  *
  * @param string
  * @param bool
  * @param  bool
  * @return void
  */
 public function external_config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
 {
  $file = ($file == '') ? 'config' : str_replace('.php', '', $file);
  $found = FALSE;
  $loaded = FALSE;

  $check_locations = defined('ENVIRONMENT')
   ? array(ENVIRONMENT.'/'.$file, $file)
   : array($file);


   foreach ($check_locations as $location)
   {
    $file_path = $file.'.php';

    if (in_array($file_path, $this->is_config_loaded, TRUE))
    {
     $loaded = TRUE;
     continue 2;
    }

    if (file_exists($file_path))
    {
     $found = TRUE;
     break;
    }
   }

   if ($found === FALSE)
   {
    if ($fail_gracefully === TRUE)
    {
     return FALSE;
    }
    show_error('The configuration file '.$file.'.php does not exist.');
   }
   else{
    include($file_path);

    if ( ! isset($config) OR ! is_array($config))
    {
     if ($fail_gracefully === TRUE)
     {
      return FALSE;
     }
     show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
    }

    if ($use_sections === TRUE)
    {
     if (isset($this->ext_config[$file]))
     {
      $this->ext_config[$file] = array_merge($this->ext_config[$file], $config);
     }
     else
     {
      $this->ext_config[$file] = $config;
     }
    }
    else
    {
     $this->ext_config = array_merge($this->ext_config, $config);
    }

    $this->is_config_loaded[] = $file_path;
    unset($config);

    $loaded = TRUE;
    log_message('debug', 'Config file loaded: '.$file_path);
   }

  return TRUE;
 }
} 

CodeIgniter对正在加载的配置文件、其位置、文件扩展等进行假设。您可以加载其他文件,即:来自同一目录(支持似乎仅限于此使用场景)

但是,您可以(查看章节(设置配置项))迭代文件值并按如下方式设置它们

(todo:添加一些检查文件是否存在、是否可读、是否没有解码错误等)

至于config对象的公开,在loader中,您可以随时使用code igniter singleton实例检索方法以或多或少微妙的方式检查其公共部分:

$CI =& get_instance();
die(print_r($CI->config,true));

这将是一个CI_配置对象。您还可以创建“我的配置”扩展。

您的配置必须存在于同一配置目录中。
$CI =& get_instance();
die(print_r($CI->config,true));