Maintenance mode improves the user experience by notifying users when the site is undergoing maintenance. When the web application requires an update, it is usually a good idea to present a well-conceived maintenance page instead of exhibiting problems on the website.

Codeigniter, itself doesn’t provide any functionality to setup maintenance mode. However, there are several options for displaying a maintenance page in Codeigniter. In this article, I’ll show you how to put your Codeigniter site into maintenance mode.

Hooks in Codeigniter allow you to change core functionalities without changing core files. You can easily setup maintenance or under construction pages using hooks in Codeigniter.

To put your site in offline mode for maintenance in CodeIgniter, follow the steps below.

Enable Hooks

First of all, you’ll need to enable hooks. Edit the application/config/config.php file and set $config['enable_hooks'] to TRUE.

Define Maintenance Config

Edit the application/config/config.php file and define a new config variable for maintenance mode.
Insert the following code at the bottom of the config.php file.

/*
|--------------------------------------------------------------------------
| Maintenance Mode
|--------------------------------------------------------------------------
| $config['maintenance_mode'] = TRUE; // site is offline
| $config['maintenance_mode'] = FALSE; // site is online
*/

$config['maintenance_mode'] = TRUE;

Define Maintenace Hook

Edit the application/config/hooks.php file and define hook to let the system know about the maintenance hook.

  • pre_system: Hook point. The hook will be called very early during the system execution.
  • class: The name of the class you’d like to call up.
  • function: The method name you wish to call.
  • filename: The file name containing the class/function.
  • filepath: The name of the directory containing the hook script.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|	https://codeigniter.com/user_guide/general/hooks.html
|
*/

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

Maintenance Hook Class

In the application/hooks/ folder, create a new hook file named maintenance_hook.php and write the Maintenance hook script. The code below checks if site maintenance mode is enabled and then loads the maintenance page from the application views folder.

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Maintenance_hook extends CI_Controller
{
    public function __construct()
    {
        log_message('debug', 'Accessing Maintenance Hook!');
    }

    public function check_maintenance_mode()
    {
        if(isset($config['maintenance_mode']) && $config['maintenance_mode'] === TRUE)
        {
            include(APPPATH . 'views/maintenance.php');
            exit;
        }    
    }
}

Maintenance Page

Create a maintenance.php file in the application/views/ directory and insert the HTML to display a well-designed maintenance page.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Site Under Maintenance</title>
    <link rel="stylesheet" href="<?php echo base_url(); ?>path-to-your-file.css" type="text/css">
</head>
<body>
    <div class="text-center">
        <h1>Site Under Maintenance</h1>
        <p>Sorry for the inconvenience. To improve our services, we have momentarily shutdown our site.</p>
    </div>
</body>
</html>

Enable/Disable Maintenance Mode

Now you can enable the maintenance mode by setting $config['maintenance_mode'] = TRUE; To disable the maintenance mode, change it to $config['maintenance_mode'] = FALSE;