Setup Maintenance Mode Using Hooks in Codeigniter

image1 (1)

There are so many ways to achieve maintenance mode in codeigniter. Codeigniter itself doesn`t provide such kind of functionality as other framework supports. This post supports how to setup maintenance mode using hooks.

Following are the steps to achieve this:

  1. Enable Hooks in the config.php file
  2. Create a custom config file for maintenance settings i.e maintenance_config.php
  3. Create an html called maintenance_view.php page under views or in root folder
  4. Create a Hook file called maintenance.php
  5. In the hooks config setup your hooks call to run on post_controller

application/views/maintenance_view.php

<!DOCTYPE html>
<html>
  <head>
    <title>Maintenance</title>
    <style>Style your page</style>
  </head>
  <body>
    <p>We apologize but our site is currently undergoing maintenance at this time.</p>
    <p>Please check back later.</p>
  </body>
</html>

application/config/maintenance_config.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['site_under_maintenance'] = TRUE;
?>

application/hooks/maintenance.php

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Maintenance {
  var $CI;
  public function maintenance() {
     $this->CI = & get_instance();
     $this->CI->config->load('maintenance_config'); // Load custom config file
     if ($this->CI->config->item("site_under_maintenance")) {
        include(APPPATH . '/views/maintenance_view.php');
        die();
     }
  }
}
?>

application/config/hooks.php

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
$hook['post_controller'][] = array(
  'class' => 'maintenance',
  'function' => 'maintenance',
  'filename' => 'maintenance.php',
  'filepath' => 'hooks',
  'params' => array()
);
?>

Output

Maintenance

Extras: Addicted Upcoming

Advertisements