2012-05-24 74 views
0

我正在運行基於php codeigniter的web應用程序。我想在我的應用程序中顯示自定義錯誤頁面和自定義錯誤消息。在Codeigniter中顯示自定義錯誤消息

所以我用自定義異常類擴展了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; 
} 

}

+0

你能解釋一下你如何確信只有一種方法可以嗎?何時調用show_php_error? –

回答

1

你爲什麼不只是改變了 「error_404.php」 和 「error_general.php」 文件/應用/錯誤?

這樣,你可以設定你想要的頁面風格,並把你的通用錯誤信息放在那裏,而不是擴展核心類?

相關問題