How to setup maintenance mode in CodeIgniter

I’m going to share with you How to configure CodeIgniter maintenance mode. Maintenance mode allows you to show a user-friendly message to your visitors during website maintenance, rather than a broken site. Often you might need a longer period of time to work on your website.

The Hooks from CodeIgniter offer a feature to change the core functionality without changing the core files.

Enable Hooks

You need to make hooks first. Edit application/config/config.php file, set $config['enable hooks'] to TRUE.

/*$config['enable_hooks'] = TRUE;

Define Maintenance Config

Goto application/config/config.php file, and set a new maintenance mode config variable. Insert next code at the end of the config.php file.

/*
|--------------------------------------------------------------------------
| Maintenance Mode
|--------------------------------------------------------------------------
|
| For whatever reason sometimes a site needs to be taken offline.
| Set $config['maintenance_mode'] to TRUE if the site has to be offline
|
| $config['maintenance_mode'] = TRUE; // site is offline
| $config['maintenance_mode'] = FALSE; // site is online
*/
$config['maintenance_mode'] = TRUE;

Defining Maintenance Hook

To let the system know about the maintenance hook, edit the application/config/hooks.php file and register the maintenance hook.

/*$hook['pre_system'][] = array(
    'class'    => 'maintenance_hook',
    'function' => 'offline_check',
    'filename' => 'maintenance_hook.php',
    'filepath' => 'hooks'
);

In the above config array,

  • pre_system: One of the hook points available in CodeIgniter. During execution of the program the hook will be called very early and it is the good point to display the maintenance tab
  • class: The class name wishes to be invoked.
  • function: The method name wish to call.
  • filename: Filename with class/function
  • filepath: Directory name with hook file.

Maintenance Hook Class

Create a new application/hooks/maintenancehook.php hook file, and add the following php code to write. The following code checks the maintenance variable $config['maintenance mode']=TRUE configuration value that we have deinfected in the application/hooks/config.php file and if that value is true, it will load the site maintenance page from the application views directory.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Check whether the site is offline or not.
 *
 */
class Maintenance_hook
{
    public function __construct(){
        log_message('debug','Accessing maintenance hook!');
    }
    
    public function offline_check(){
        if(file_exists(APPPATH.'config/config.php')){
            include(APPPATH.'config/config.php');
            
            if(isset($config['maintenance_mode']) && $config['maintenance_mode'] === TRUE){
                include(APPPATH.'views/maintenance.php');
                exit;
            }
        }
    }
}

Maintenance View Page

Create a new maintenance.php file in the application/views/ directory and you can create your html view file.

<!doctype html>
<html lang="">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Maintenance</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <!-- Styles -->
</head>

<body>
     <div class="container">
        <div class="jumbotron jumbotron-fluid">
        <div class="container text-center">
        <h1 class="display-4 ">Site Under Maintenance</h1>
        <p class="lead">Sorry for the inconvenience. To improve our services, we have momentarily shutdown our site.</p>
  </div>
</div>
    </div>
</body>

</html>

Leave a Reply

Your email address will not be published. Required fields are marked *