【问题标题】:codeigniter load external config filecodeigniter 加载外部配置文件
【发布时间】: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


【解决方案1】:

CodeIgniter 对正在加载的配置文件、它的位置、它的文件 ext 等做出假设。您可以从同一目录加载其他文件(支持似乎仅限于此使用场景)。

但是,您可以(查看章节 https://ellislab.com/codeigniter/user-guide/libraries/config.html(设置配置项))迭代您的文件值并像这样设置它们

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

$myConfigValues = json_decode(file_get_contents($configKeyValuesFromOuterSpace));
foreach($myConfigValues as $key => $value){
  $CI->config->set_item('item_name', 'item_value');
}

至于 config 对象的暴露,在加载器中,您可以随时使用代码点火器单例实例检索方法或多或少微妙的方式检查其公共部分:

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

这将是一个 CI_Config 对象。您也可以创建一个 MY_Config 扩展。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多