Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter not loading extended MY_Exceptions

I'm trying to override show_404 method from CI_Exceptions, but MY_Exceptions is never loaded.

MY_Exceptions is located at application/core/

It's code

<?php
class MY_Exceptions extends CI_Exceptions {
    function show_404(){
        header("HTTP/1.1 404 Not Found");
        $CI =& get_instance();
        $CI->load->view('header.php');
        $CI->load->view('err/custom404.php');
        $CI->load->view('footer.php');
    }
}

When I call show_404() I get two Fatal error

Fatal error: Class 'MY_Exceptions' not found in C:\workspace\ictp-tv-main\system\core\Common.php on line 196

Fatal error: Class 'MY_Exceptions' not found in C:\workspace\ictp-tv-main\system\core\Common.php on line 196

I have other extended classes that work well, with the same prefix MY_

EDIT After @Tpojka suggestion my code is

class MY_Exceptions extends CI_Exceptions {

    public function __construct()
    {
        parent::__construct();
        $this->CI =& get_instance();
    }

    public function show_404($page = '', $log_error = TRUE){

        header("HTTP/1.1 404 Not Found");

        $this->CI->load->view('header.php');
        $this->CI->load->view('err/custom404.php');
        $this->CI->load->view('footer.php');

    }
}

And now the 404 page is blank.

EDIT

SOLUTION

We need to ECHO the error, not simple load view.

public function show_404($page = '', $log_error = TRUE){
    header("HTTP/1.1 404 Not Found");
    echo $this->CI->load->view('header.php',array('PAGE_TITLE'=>'Page not found'),true);
    echo $this->CI->load->view('err/custom404.php',null,true);
    echo $this->CI->load->view('footer.php',null,true);
    exit(4);
}

Thanks @Tpojka to open my mind.

like image 575
lcssanches Avatar asked Oct 20 '25 04:10

lcssanches


1 Answers

Try this way

<?
class MY_Exceptions extends CI_Exceptions
{
    public function __construct()
    {
        parent::__construct();
    }

    public function show_404($page = '', $log_error = TRUE)
    {
        if (is_cli())
        {
            $heading = 'Not Found';
            $message = 'The controller/method pair you requested was not found.';
        }
        else
        {
            $heading = '404 Page Not Found This Time';
            $message = 'The page you requested was not found.';
        }
        // By default we log this, but allow a dev to skip it
        if ($log_error)
        {
            log_message('error', $heading.': '.$page);
        }
        echo $this->show_error($heading, $message, 'custom404', 404);//custom404 is in APPPATH.'views/errors/html/custom404.php'
        exit(4); // EXIT_UNKNOWN_FILE
    }
}

I edited code above. I took research and ended with this. I just copied show_404() method from CI_Exceptions class and saw what changes could be done. You can set your message, your header and call your template as well. You can play this way. Also I would suggest you to place $this->load->view('header') and $this->load->view('footer') in custom404.php file itself. In file custom404.php file just echo $heading and $message.

like image 193
Tpojka Avatar answered Oct 22 '25 16:10

Tpojka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!