Display Maintenance Mode Using Hook in Codeigniter Application

Display Maintenance Mode Using Hook in Codeigniter Application

For our web application like business application, E-commerce application or any other custom web application, maintenance is more important to increase productivity, customer attraction and  other changes that it needs to be up to date.

In this tutorial we will see how to set maintenance mode using hook in codeigniter without modifying other core files. It is easy to use.

Explanation of Hooks:

Hooks simply executes a script with specific path in Codeigniter execution process without modifying the core files of codeigniter.

How to Enable Hook in codeigniter ?

Without enabling hooks in config we cannot use hooks.Hooks can be enable globally. To enable hooks you need to set true in application/config/config.php as I am showing below.

/*
|--------------------------------------------------------------------------
| Enable/Disable System Hooks
|--------------------------------------------------------------------------
|
| If you would like to use the 'hooks' feature you must enable it by
| setting this variable to TRUE (boolean). See the user guide for details.
|
*/
$config['enable_hooks'] = TRUE;

How to Define a hook ?

Its easy to define a hook. To define a hook we will use hook point. There are many hook point available in codeigniter but we are going to use pre_system hook point to call our class and method very early during system execution.Please find below the code to define a hook inside application/config/hooks.php file.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$hook['pre_system']= array(
'class'=>'maintenance_hook',
'function'=>'mentainMode',
'filename'=>'maintenance_hook.php',
'filepath'=>'hooks'
);

Note: We can create multiple script with the same hook with the array declaration of hook.Multiple hook permit you to have same hook point with multiple script.Execution order depends on your hook define order.

Class declaration of Hook:

Create a file named Mentanance_hook.php inside application/hooks directory.This file is to declare class and the function within the class. This class will invoke from hook.Here is the code.

Mentanance_hook.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Maintenance_hook {
publicfunction mentainMode(){
require_once APPPATH.'views/maintenance.php'; ## call view
exit;
}
}
?>

Maintenance View:

Next, Create a view file named maintenance.php inside application/views directory to show a page when site is in maintenance mode.

maintenance.php

<!DOCTYPE html>
<html lang="en">
<head>
<metacharset="utf-8">
<title>Site Under Maintenance</title>
</head>
<body>
<h1>We'll be up and running Soon...</h1>
<div>
<div>
<p>We're just doing some nerdy stuff to our Website to make it even better.</p>
</div>
</div>
</body>
</html>

Enable/Disable Maintenance Mode:

That’s all, Now you can test it by running application.To Enable/Disable maintenance mode of your codeigniter application by changing the value of enable_hooks with true or false.

Maintenance Mode View

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!