codeigniter 3.0 custom 404 not found error page

后端 未结 4 691
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 11:53

My problem is about custom error pages after upgrading to Codeigniter 3.0.

I used to have a Errors controller which handled 404 error page of the websi

相关标签:
4条回答
  • 2021-01-05 11:59

    In Codeigniter 3, instead of calling show_404(), I just redirected to the custom error controller function.

    e.g. for the above scenario

    if (404 condition...) {
        redirect("my404");
    }
    
    0 讨论(0)
  • 2021-01-05 12:17

    It's not working in codeigniter 3, I overwrite the default view (application/views/errors/html/error_404.php) and added my custom view, now show_404() is working fine.

    0 讨论(0)
  • 2021-01-05 12:22

    that is working good, but if redirect to an error, for example 404 with function show_404, your controller wont load your view, for a real 404 in the show_404 method, you have to go to the core of codeigniter and touch it.

    \system\core\Common.php

    this is an example of code:

    function show_404(){
      $ci = get_instance();
      $ci->output->set_status_header(404);
      $ci->load->view('errors/error404_view');
      echo $ci->output->get_output();
      exit(4);
    }
    
    0 讨论(0)
  • 2021-01-05 12:23

    You need to set in application/config/routes.php the following route

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

    Then you need to create a new controller in your controllers directory (application/controllers/)

    <?php 
    class My404 extends CI_Controller 
    {
     public function __construct() 
     {
        parent::__construct(); 
     } 
    
     public function index() 
     { 
        $this->output->set_status_header('404'); 
        $this->load->view('err404');//loading in custom error view
     } 
    } 
    

    And create an err404 view. Thats all!

    Refer :Reserved Routes

    0 讨论(0)
提交回复
热议问题