在Codeigniter中显示自定义错误消息

时间:2012-05-24 10:58:21

标签: php codeigniter

我正在运行基于php codeigniter的Web应用程序。我想在我的应用程序中显示自定义错误页面和自定义错误消息。

所以我用自定义异常类扩展了codeigniter的核心Exception类。我在application / core文件夹中添加了一个文件MY_Exceptions.php。

似乎覆盖了show_error方法,但我的自定义类不会覆盖show_php_error。它仍在执行system / core / Exceptions.php中的show_php_error方法,而不是application / core / MY_Exceptions.php(我的自定义类)。

MY_Exceptions.php文件如下 -

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions
{
public function __construct()
{
    parent::__construct();
}

function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
    if($template!='error_404'){
        $template = 'custom_error_page';
        $heading = "An Error Occured";
        $message = "We're sorry, but we can not complete the action you requested. A notification has been sent to our customer service team. Please try again later.";
    }else{
        $template = 'custom_error_page';
        $heading = "404 Page Not Found";
        $message = "We're sorry, but we can not load the page you requested. A notification has been sent to our customer service team. Please try again later..";
    }
    set_status_header($status_code);

    $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

    if (ob_get_level() > $this->ob_level + 1)
    {
        ob_end_flush();
    }
    ob_start();
    include(APPPATH.'errors/'.$template.'.php');
    $buffer = ob_get_contents();
    ob_end_clean();
    return $buffer;
}

function show_php_error($severity, $message, $filepath, $line)
{
    $heading = "An Error Occured";
    $message = " We're sorry, but we can not complete the action you requested. A notification has been sent to our customer service team. Please try again later.";
    echo $this->show_error($heading, $message, 'custom_error_php', 404);
    exit;
}

}

1 个答案:

答案 0 :(得分:1)

为什么不改变/ application / errors中的“error_404.php”和“error_general.php”文件?

通过这种方式,您可以根据需要设置页面样式,并将常规错误消息放在那里,而不是扩展核心类?