在CodeIgniter中扩展异常

时间:2012-07-24 02:32:23

标签: php codeigniter

我收到错误致命错误:在第233行的.... \ core \ CodeIgniter.php中找不到类'CI_Controller'

我在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;
}
}

请帮助!!!

2 个答案:

答案 0 :(得分:4)

Exceptions类在主CI_Controller之前加载。

这意味着发生错误时 - 您无法使用CI发送电子邮件,因为“$ this-&gt; CI”不存在。

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

答案 1 :(得分: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;
    }
}
?>