2012-07-24 106 views
0

我有错误致命错误:类“是CI_Controller”在.... \没有找到核心\ CodeIgniter.php上线233扩展异常CI中

我创建了一个称为应用程序/核心/ My_Exception My_Exceptions类。 php 基本上,我想在用户从我的网站获取错误时收到电子邮件。

<?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('[email protected]', 'APP Error'); 
    $this->CI->email->to('[email protected]'); 
    $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

Exceptions类在主CI_Controller之前加载。

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

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

+0

这似乎很疯狂得到一个电子邮件*获取生成每个* 404,发送电子邮件或手动检查每天的日志更有意义(我能理解不想创建cron作业)。本地'mail()'应该没问题,当你只是给自己发送电子邮件时,这没什么大不了的。 – 2012-07-24 03:25:00

+1

我认为他是在其他错误之后,而不仅仅是404的 – Laurence 2012-07-24 04:01:43

+0

OP的代码被写入'show_404'函数,但你可能是对的。在这种情况下,我会说,为每个错误发送一封单独​​的电子邮件会更加疯狂,因为会有更多。除非你真的希望你的收件箱像疯了一样疯狂......单页加载可以生成*吨* 404(例如缺少图片)。 – 2012-07-24 04:04:34

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 = '[email protected]'; 
    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 <[email protected]>' . "\r\n"; 
      $headers .= 'Content-type: text/plain; charset=utf-8\r\n'; 

      mail($to, $subject, $message, $headers); 
     } 
     return $result; 
    } 
} 
?> 
+0

这是另一种选择。谢谢 – Sml004 2012-07-30 03:56:34