0

我收到错误 Fatal error: Class 'CI_Controller' not found in ....\core\CodeIgniter.php on line 233

我在 application/core/My_Exception.php 中创建了一个名为 My_Exceptions 的类 基本上,我想在用户从我的站点收到错误时收到电子邮件。

<?php
class My_Exceptions extends CI_Exceptions{
    var $CI="";
    function __construct(){
        parent::__construct();
        $this->CI =& get_instance();
    }
    function show_404($page = '', $log_error = TRUE)
{
    $heading = "404 Page My Not Found";
    $message = "The page you requested was not found.";

    // By default we log this, but allow a dev to skip it
    if ($log_error)
    {
        log_message('error', '404 Page Not Found --> '.$page);
    }

    //Email to Developer
    $this->CI->load->library('email');
    $uri = $this->CI->uri->uri_string();  
    $this->CI->email->from('error-donotreply@YOURAPP.com', 'APP Error');
    $this->CI->email->to('youremail@example.org');
    $this->CI->email->subject('APP Error [severity: '.$severity.']');
    $this->CI->email->message("Page not Found. From URL: ".$uri);
    $this->CI->email->send();

    echo $this->show_error($heading, $message, 'error_404', 404);
    exit;
}
}

请帮忙 !!!

4

2 回答 2

4

我知道您已经接受了答案,但另一种方法是扩展 CI 日志类。

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Log Class
 *
 * This library extends the native Log library.
 * It adds the function to have the log messages being emailed when they have been outputted to the log file.
 *
 * @package     CodeIgniter
 * @subpackage      Libraries
 * @category        Logging
 * @author      Johan Steen
 * @link        http://wpstorm.net/
 */
class MY_Log extends CI_Log {
    private $_reporting_email = 'test@me.com';
    private $_subject = 'CI Logger';

    /**
     * Constructor
     *
     * @access  public
     */
    function __construct() {
        parent::__construct();
    }

    /**
     * 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);

        if ($result == TRUE && strtoupper($level) == 'ERROR') {
            $message = "An error occurred: \n\n";
            $message .= $level.' - '.date($this->_date_fmt). ' --> '.$msg."\n";

            $to = $this->_reporting_email;
            $subject = $this->_subject;
            $headers = 'From: Example Name <no-reply@example.com>' . "\r\n";
            $headers .= 'Content-type: text/plain; charset=utf-8\r\n';

            mail($to, $subject, $message, $headers);
        }
        return $result;
    }
}
?>
于 2012-07-24T07:18:56.383 回答
4

Exceptions 类在主 CI_Controller 之前加载。

这意味着当发生错误时 - 您不能使用 CI 发送电子邮件,因为“$this->CI”不存在。

您的选择是使用本机 PHP 发送电子邮件,或者我做什么;自动将前一天的错误日志通过电子邮件发送给自己(使用 CRON 作业)。这样您就可以每天检查一次所有错误。

于 2012-07-24T03:18:50.963 回答