开发者

CodeIgniter - Using load->view() in Exceptions

开发者 https://www.devze.com 2022-12-17 04:47 出处:网络
I want my error_404.php to show up in my site template views.I have extended the CI_Exceptions class (MY_Exceptions) and overridden the show_404() and show_error() methods.Now what I want to do is be

I want my error_404.php to show up in my site template views. I have extended the CI_Exceptions class (MY_Exceptions) and overridden the show_404() and show_error() methods. Now what I want to do is be able to load a view file in there. Optimally, I would like to load the _header() and _footer() methods in MY_Controller class. Is this possible somehow?

class MY_Exceptions extends CI_Exceptions {

  public function __construct(){
    parent::__construct();
  }

  function show_404($page = '')
    {
        $heading = "404 Page Not Found";
        $message = "The page you requested was not found for some strange reason...";

        log_message('error', '404 Page Not Found --> '.$page);

        $CI =& get_instance();
        $CI->load->view('template/header');
        echo $this->show_error($heading, $message, 'error_404', 404);
        $C开发者_如何学CI->load->view('template/footer');
        exit;
    }

    function show_error($message, $status_code = 500)
    {
        $error =& load_class('Exceptions');
        echo $error->show_error('An Error Was Encountered', $message, 'error_general', $status_code);
        exit;
    }
}  

But I cannot do this. Any suggestions?


Use this in your controller.

$data['main'] = 'my404';
$this->load->vars($data);
$this->load->view('maintemplate');

And in the maintemplate view.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

what ever here.


</head>
<body>
<div id="wrapper">
  <div id="header">
  <?php $this->load->view('admin_header');?>
  </div>

  <div id="main">
  <?php $this->load->view($main);?>
  </div>

  <div id="footer"> 
  <?php $this->load->view('admin_footer');?>
  </div>
</div>


</body>
</html>

And create your my404 view.

0

精彩评论

暂无评论...
验证码 换一张
取 消