【问题标题】:Extend Exception in CodeIgniter在 CodeIgniter 中扩展异常
【发布时间】:2012-07-22 07:47:28
【问题描述】:

我收到错误 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;
}
}

请帮忙!!!

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    Exceptions 类在主 CI_Controller 之前加载。

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

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

    【讨论】:

    • 为生成的每个 404 收到一封电子邮件似乎很疯狂,通过电子邮件发送或手动查看每日日志更有意义(我可以理解不想创建 cron 作业)。原生mail() 应该没问题,当你只是给自己发电子邮件时,这没什么大不了的。
    • OP 的代码被写入show_404 函数,但你可能是对的。在这种情况下,我会说为每个错误发送单独的电子邮件更加疯狂,因为会有更多错误。除非您真的希望收件箱像疯了一样炸毁...单个页面加载会产生 的 404(例如缺少图像)。
    • 不仅仅是 404 错误。我将添加到另一个错误处理程序。 404 很容易在我的本地主机上测试。谢谢,我要试试 PHP native。
    【解决方案2】:

    我知道你已经接受了答案,但另一种方法是扩展 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;
        }
    }
    ?>
    

    【讨论】:

    • 这是另一种选择。谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多