【发布时间】:2014-05-13 21:12:34
【问题描述】:
我正在使用 CodeIgniter,我想加载一个或多个位于外部文件夹中的配置文件,由不同的 CI 安装共享。 有可能吗?
我尝试扩展 Loader 类,并调用新方法: $this -> 加载 -> external_config('MY\EXTERNAL\PATH');
加载成功,但我无法在控制器中检索配置项,因为在 MY_Loader 类中 core\Loader 配置属性不可见,我无法将其与新加载的值合并。
这是我的代码:
<?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;
}
}
我在 core\Config.php 中找到了一个属性 var $_config_paths = 数组(APPPATH);
使用路径查找配置文件。 如何在没有核心类代码的情况下向数组添加路径? 有任何想法吗? 非常感谢!!!
【问题讨论】:
-
您的配置必须存在于同一个配置目录中。
标签: codeigniter config loader