random425
random425

Reputation: 719

Codeigniter 3 show_404 function issue - MY_Exception doesn't load

I'm using Codeigniter 3 and I want to custom the show_404() function.

Here is my main controller:

class Welcome extends CI_Controller {

    public function index() {
        $this->load->view('welcome_message');
    }

    public function otherMethod() {
        show_404();
    }

}

Added on my routes.php

$route['404_override'] = 'error/show_404_custom';

Created the controller Error.php with the method show_404_custom

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Error extends CI_Controller {

    public function show_404_custom() {
        $this->load->view('404_read_view');
    }
}

The 404_read_view.php view:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<p>Custom 404 page.</p>
</body>
</html>

Then I've tested with some undefined URL like:

/index.php/welcome/method_A 

And I successfully get my Custom 404 page.

However, if I run a method that uses the show_404() function like:

/index.php/welcome/otherMethod 

I oddly get the standard Codeigniter 404 page.

I've searched a bit and found out that I should Extend the Exception core class and override the show_404() function like this:
https://stackoverflow.com/a/8425433/4301970

So, I created the MY_Exceptions.php class inside /application/core:

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class MY_Exceptions extends CI_Exceptions {

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


    public function show_404() {
        $CI =& get_instance();
        $CI->load->view('404_read_view');
        echo $CI->output->get_output();
        exit;
    }

}

And when I run the URL:

/index.php/welcome/otherMethod 

I get the error:

Fatal error: Class 'MY_Exceptions' not found in (...)\system\core\Common.php on line 196

I took look inside Common.php and noticed that the load class function looks inside the libraries directory instead of core directory?

function &load_class($class, $directory = 'libraries', $param = NULL)

How can I solve this problem?

Upvotes: 1

Views: 3498

Answers (2)

ttpryg
ttpryg

Reputation: 160

use this, it's work in CI 3.1.4 (my latest project)

class MY_Exceptions extends CI_Exceptions {

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

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

        $CI =& get_instance();
        $CI->load->view('404_read_view');
        echo $CI->output->get_output();
        exit;
    }
}

Upvotes: 2

user4419336
user4419336

Reputation:

When you need to use CI3 404_override cannot be in subfolder

Change

$route['404_override'] = 'error/show_404_custom'; 
// thinks its in subfolder

To

Location: application > controllers > Error.php

$route['404_override'] = 'error';

Controller

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Error extends CI_Controller {

    public function index() { // change to index
        $this->load->view('404_read_view');
    }
}

Upvotes: 0

Related Questions