【问题标题】:Codeigniter: Get custom config file from a custom lib?Codeigniter:从自定义库中获取自定义配置文件?
【发布时间】:2013-02-28 14:18:42
【问题描述】:

我正在扩展 CodeIgniters 核心库(日志),具有更好的阈值级别和发送严重错误的方法。但由于某种原因,我无法从我的库中读取我的配置文件。如果我无法访问我的配置....我无法获取邮件地址。我究竟做错了什么?我得到的消息是:“致命错误:在...中找不到类 'CI_Controller'”

这是我目前的代码:

class MY_Log extends CI_Log {
/**
 * Constructor
 *
 * @access  public
 */
function MY_Log()
{
    parent::__construct();
    $this->_levels  = array('ERROR' => '1', 'INFO' => '2',  'DEBUG' => '3', 'ALL' => '4');
}

/**
 * Write Log File
 *
 * Calls the native write_log() method and then sends an email if a log message was generated.
 *
 * @access  public
 * @param   string  the error level
 * @param   string  the error message
 * @param   bool    whether the error is a native PHP error
 * @return  bool
 */
function write_log($level = 'error', $msg, $php_error = FALSE)
{
    $result = parent::write_log($level, $msg, $php_error);

    $this->ci =& get_instance();
    $this->ci->load->config('myconf/mailconf');
    echo $this->ci->config->item('emails');
}
}

【问题讨论】:

  • 见 - stackoverflow.com/a/8774159/50913 - 它可能有用。
  • 这个答案已经有一年多了。它对我没有帮助。我无法从 MY_Log 加载任何内容。没有配置,没有库。没有。我唯一能做的就是用这个访问主配置文件: $config =& get_config();这样可行。但这不是我的配置。而且我仍然想在我的库中加载“电​​子邮件”库。

标签: php codeigniter frameworks


【解决方案1】:

我认为这是因为此类 (Log/MY_Log) 在应用程序生命周期中加载得太早,而 get_instance() 不可用。

也许您可以编写自己的类来处理此类日志记录,而不是扩展日志记录功能的构建(如果您这样做,您可以将其包装并以spark 的形式发布:)

或者,您可以使用composer package 为您提供您所追求的功能,因为我确信已经存在一个。

如果您不熟悉作曲家,我建议您阅读Phil Sturgeon's article

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-15
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多