How to create a 404 page in CodeIgniter

Leave a Comment
This post is about creating a custom 404 page in CodeIgniter. There are so many reasons for having 404 page. Because you cannot avoid user typing incorrect URL pointing to a page on your domain which doesn't exists. Also there is some website can link to some url on website which no longer valid.

Codeigniter provides it's own 404 page which is ok. But we can create our own custom 404 page.CodeIgniter provides an easy way to build a custom 404 (Page-not-found) page. Here is the step by step on setting up a 404 page for your CodeIgniter website.

1. Create a new Controller


Create a new controller custom404.php. And Paste below code
<?php
class custom404 extends CI_Controller
{
    public function __construct()
    {
            parent::__construct();
    }

    public function index()
    {
        $this->data['content'] = 'custom404_view';  // View name
        $this->load->view('template', $data);   // load viwe
    }
}
?>
    

2. Create a view


Now create a view or your custom 404 page html design the way you like.

3. changes in routes.php


Now edit your routes.php which is present in application/config/routes.php. Change the following line in file
$route['404_override'] = 'custom404'; // Change your Controller name
    

Now try to put wrong url on browser you will get you custom 404 page.
Enjoy......:)
Powered by Blogger.