How to Create Custom Codeigniter 404 Not Found Page
404 Not Found is perhaps the most common type of error users encounter on the internet. It is one of the most common errors users experience during web browsing. Since the error could potentially impact the user experience, CMS and frameworks have come up with creative ways of handling the error.
Codeigniter offers a simple way of creating custom 404 error pages in which the website or the application could tackle the situation creatively and redirect the visitor to the appropriate page(s). This way, the developers have complete control over the user journey and could make sure that the visitor doesn’t bounce off the website. Codeigniter 404 Page Not Found error offers a great opportunity to retain user engagement and present related links and/or information about services or products to the visitors.
Why 404 Not Found Error Occurs
404 Not Found is the HTML error code that indicates a particular situation in which the connection between the server and the client is working correctly, but the server could not find the resource requested by the client.
The underlying causes of the error are several, but the general process is similar in all cases. The server has received a request for a resource that is non-existent on the server of PHP hosting. A very common cause of this situation is the broken links that point to pages or resources that have been removed (or taken down) by the website administrator.
In this article, I will show you how you can create a fully customized 404 error page in Codeigniter. I will start by creating the controller and then move on to set up the routes.
Create the Controller for Custom Codeigniter 404 Page Not Found Error
I will start by creating a class for the controller in the application/controllers/ folder. I have named this class My404 which has an index() method for loading the view.
- <?php
- class My404 extends CI_Controller
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function index()
- {
- $this->output->set_status_header('404');
- $this->load->view('err404'); }
- }
Set up the Route
Next, I will set up the route in the application/config/routes.php file. This file will contain the following line that overrides the default route:
- $route['404_override'] = 'my404';
Creating the View
I will now describe the view of the custom 404 page in the application/views folder. you will notice that in the following complete code of the view that I have redirected the visitor to the homepage.
- <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
- <?php
- $ci = new CI_Controller();
- $ci =& get_instance();
- $ci->load->helper('url');
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>404 Page</title>
- </head>
- <body>
- <div>
- <p>How to Create Custom Codeigniter 404 Error Pages </p>
- <div>
- <p><a href="<?php echo base_url(); ?>">Go Back to Home</a></p>
- </div>
- </div>
- </body>
- </html>
Alternative Method of Creating the Controller
Another (and much simpler) way of creating a custom 404 page for your Codeigniter project is to add the following code to the system/application/controllers/error.php file. In this code, I have extended the controller class into a custom class named Error. This class contains the method error_404() that outputs the “404 – Not Found” error if it encounters the 404 code in the header.
- <?php
- class Error extends Controller {
- function error_404()
- {
- $this->output->set_status_header('404');
- echo "404 - not found";
- }
- }
Now create MY_Router.php in the system/application/libraries/ folder and add the following code to it:
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class MY_Router extends CI_Router {
- var $error_controller = 'error';
- var $error_method_404 = 'error_404';
- function My_Router()
- {
- parent::CI_Router();
- }
- function _validate_request($segments)
- {
- if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
- {
- return $segments;
- }
- if (is_dir(APPPATH.'controllers/'.$segments[0]))
- {
- $this->set_directory($segments[0]);
- $segments = array_slice($segments, 1);
- if (count($segments) > 0)
- {
- if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
- {
- return $this->error_404();
- }
- }
- else
- {
- $this->set_class($this->default_controller);
- $this->set_method('index');
- if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
- {
- $this->directory = '';
- return array();
- }
- }
- return $segments;
- }
- return $this->error_404();
- }
- function error_404()
- {
- $this->directory = "";
- $segments = array();
- $segments[] = $this->error_controller;
- $segments[] = $this->error_method_404;
- return $segments;
- }
- function fetch_class()
- {
- $this->check_method();
- return $this->class;
- }
- function check_method()
- {
- $ignore_remap = true;
- $class = $this->class;
- if (class_exists($class))
- {
- $class_methods = array_map('strtolower', get_class_methods($class));
- if($ignore_remap && in_array('_remap', $class_methods))
- {
- return;
- }
- if (! in_array(strtolower($this->method), $class_methods))
- {
- $this->directory = "";
- $this->class = $this->error_controller;
- $this->method = $this->error_method_404;
- include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
- }
- }
- }
- function show_404()
- {
- include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
- call_user_func(array($this->error_controller, $this->error_method_404));
- }
- }
Finally, I will call the 404 Page manually by calling the show_404() method that checks the two parameters passed in its array and shows error if it discovers that a parameter is missing. Use the following statement below to manually call the 404 Page.
- $this->router->show_404();
Conclusion
In this short tutorial, I have demonstrated two methods that you can use for creating (and calling) a custom designed 404 error page for your Codeigniter application. A custom 404 error page is an ideal way of retaining visitors on the website and redirecting them to appropriate pages on the website. This greatly reduces the bounce rate of the websites and ensures that the visitors remain interested in your website. If you have further questions and queries about creating custom 404 pages for Codeigniter projects, do post them in the comments below.
Comments
Post a Comment