如何在 CodeIgniter 中获取 show_error() 以加载视图


How to get show_error() in CodeIgniter to load a view?

我需要在所有页面中加载头视图和脚视图。但是show_error使用自己的完整模板。它在加载此模板后停止执行,因此如果我想加载页脚,它甚至不会加载。

我应该覆盖此方法还是有其他方法?

所以,这就是我的做法,它工作得很好。我按照Yan Berk的建议扩展了CI_Exceptions,但我能够使用get_instance并加载我的观点。

class MY_Exceptions extends CI_Exceptions {
private $CI;
private $dist;
public function __construct() {
    parent::__construct();
    $this->CI =& get_instance();
    $this->dist = $this->CI->config->item('dist');
}
function show_error($heading, $message, $template = 'error_general', $status_code = 500) {
    $head_data = array("title"=>$this->dist['site_title'].": Error");
    $head = $this->CI->load->view('head', $head_data, TRUE);
    $body = $this->CI->load->view('error_body', $message, TRUE);
    echo $head;
    echo $body;  
}
}
您可以

application/errors 目录中的错误消息创建自定义布局。

编辑:

如果要使用现有文件,可以执行以下操作之一:

  1. 如果只需要 404 错误页面,则可以设置自定义路由在$route['404_override']config/route.php.
  2. 如果需要处理所有错误消息,请使用MY_Exceptions并使用纯 PHP 扩展 CI_Exceptions 类,将用户重定向到相应的页面。异常类在控制器之前加载,因此不能使用 get_instance()