Monday, October 21, 2013

Custom 404 page in codeigniter

By default CodeIgniter handles broken links(404), but when it comes to look and feel the default design becomes unpleasant.   In CodeIgniter we can override the default 404 functionality and implement our own custom 404 page very easily. The steps are described below:


Step 1: First create a new controller to handle the 404 page requests. For that just create a new controller under your controllers directory. For example, Say 'custom404Page'.
The controller would look like this 

class 
custom404Page extends CI_Controller {
 public function __construct(){
           parent::__construct();        
  }   
 public function index(){
  $this->output->set_status_header('404');
  $data['errorMessage'] = ' Ooops! Page not found'; 
  $this->load->view('custom404View',$data);//loading view
 }
}

Step 2: Open routes.php  under /application/config/ directory.
Step 2: Find the line which looks like $route['404_override'] = ''. If you cannot find such a line don't worry just add the above code in that file and point your controller file. For example in our case the code will be as below:

 $route['404_override'] = 'custom404Page'


Step4: Now create the view with your design and its all set and ready to roll. 

Another way for handling broken links is to override the Exception core class in CodeIgniter. There is a function called show_404() in CodeIgniter. You can either follow the method described in the above link or follow the following method.  

Step 1: Open config.php  under /application/config/ directory.
Step 2: Ensure the following line is present in config.php
$config['subclass_prefix'] = 'MY_';
Step 3: Now create a new file under /application/libraries/. Say MY_Exception and put the following code in it:

class MY_Exceptions extends CI_Exceptions{

    function MY_Exceptions(){

        parent::CI_Exceptions();
    }

    function show_404($page=''){    

        $this->config =& get_config();
        $baseUrl = $this->config['base_url'];
        header("location: ".$baseUrl.'error.html');//view file location
        /* Use this section if your view file is under views directory
          $CI = &get_instance();
$CI->load->view('custom404View', $data);
       */
        exit;
    }

}


Step 4: Now you can use show_404 function in any of your controllers. for example 

class test extends CI_Controller{
   public function index(){
         if(some condition){
            //do something
         }else{
             show_404();
        }
   }
}

5 comments:

  1. I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog!!!!
    Canada’s Premier Web Marketing /SEO /SMM /SEM Company!

    ReplyDelete
  2. Keep doing your great job and always gain my support. This is a great article thanks for sharing this informative information here - First Canadian Aligning R&D for SR&ED Tax Credits

    ReplyDelete
  3. Thanks but not working on my CI 3 dictionary website, I don't know why.

    ReplyDelete
  4. You can also create a controller and view separately to create 404 page in codeigniter. Load the view from controller and set the route. This will enable codeigniter to show your custom 404 page whenever codeigniter 404 page not found error occurs.

    ReplyDelete