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

天大地大妈咪最大 提交于 2019-12-31 02:57:27

问题


I need to load a head view and a foot view in all my pages. But show_error uses its own complete template. And it stops execution after it loads this template, so the footer wouldn't even load if I wanted to.

Should I override this method or is there another way?


回答1:


You can create a custom layout to the error messages in the application/errors directory.

EDIT:

If you want to use your existing files you can do one of the following:

  1. If you only require the 404 error page, you can set a custom route in config/route.php under $route['404_override'].
  2. If you need to handle all error messages, extend the CI_Exceptions class with MY_Exceptions and using plain PHP, redirect the user to the appropriate page. The exceptions class is loaded before the controller so you cannot use get_instance().



回答2:


So, this is how I did it, and it worked perfectly. I extended CI_Exceptions as suggested by Yan Berk, but I was able to use get_instance AND load my view.

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


来源:https://stackoverflow.com/questions/20008767/how-to-get-show-error-in-codeigniter-to-load-a-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!