0

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

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

似乎该show_error方法已被覆盖,但show_php_error我的自定义类并未覆盖该方法。它仍在执行来自 system/core/Exceptions.php 而不是 application/core/MY_Exceptions.php (我的自定义类)的 show_php_error 方法。

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

}

4

1 回答 1

1

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

That way you can style the pages however you want, and put your generic error messages there, rather than extending a core class?

于 2012-05-24T13:51:22.363 回答