2
votes

i try to use custom 404 page from this tutorial http://maestric.com/doc/php/codeigniter_404

my controller error.php :

class Error extends Controller{


     function error_404()
     {
      $CI =& get_instance();
      $CI->output->set_status_header('404');
      echo "error bro";

     }
    } 

when i open link localhost/mading/admin/blablabla , where there is not function blablabla() in controoler admin. the output : “error bro”

i try change line in method error_404() become the code below,

class Error extends Controller{

 function error_404()
 {

  $CI =& get_instance();
  $CI->output->set_status_header('404');
  $data['title'] = "404 Page Not Found";
  $data['body'] = $CI->load->view('web/404','',true);
  $CI->load->view('web/web_page',$data);
 }
} 

but, when i open link localhost/mading/admin/blablabla , where there is not function blablabla in controller admin. the output : blank page. the function error_404 not load the view . why the 404 page not load when i open controller admin/blablabla ??

thanks

2
This may be a silly question, but are you sure you have views named 404 and web_page located in your views directory?Colin Brock
yes, i sure. because when i call $this->router->show_404(); in function , the 404 page is loaded...Zulkifli Said

2 Answers

0
votes

Reading the comments of that tutorial, many people are having the same problem with that code. Did you change parent::CI_Router() on line 10 to parent::__construct()?

But why not just set the $route['404_override'] variable?

0
votes

Just set a proper route in your config/routes.php:

$route['404_override'] = 'your_controller/show_404';

And the corresponding controller would look like that:

class Your_controller extends CI_Controller {

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

    function show_404() {
        $this->output->set_header("HTTP/1.1 404 Not Found");
        $this->load->view ('common/errors/404'); //wherever your view is
    }
}

That should do the trick for you.