Google trends Public Holidays Coupon Code Code Compiler

How to Setup Maintenance Mode in CodeIgniter Using Hooks


Feb 12, 2024

How to Setup Maintenance Mode in CodeIgniter Using Hooks

Learn how to use hooks in CodeIgniter to create a custom maintenance mode page for your web application. This tutorial will show you how to enable and disable maintenance mode with a simple config variable.

CodeIgniter is a popular PHP framework that offers a simple and elegant way to build web applications. However, sometimes you may need to take your site offline for maintenance or updates. In such cases, it is a good practice to display a friendly and informative message to your visitors, instead of showing them errors or broken pages.

In this tutorial, we will show you how to setup maintenance mode in CodeIgniter using hooks. Hooks are a feature of CodeIgniter that allow you to modify the core functionality of the framework without changing the core files. You can use hooks to execute custom code at various points of the application execution.

We will use a pre_system hook, which is called very early during the system execution, to check if the site is in maintenance mode or not. If it is, we will load a custom view file that contains the maintenance message. Otherwise, we will let the application run normally.

To follow this tutorial, you need to have CodeIgniter installed and configured on your web server. You also need to have a basic understanding of CodeIgniter's MVC architecture and configuration files.

Step 1: Enable Hooks

The first step is to enable hooks in CodeIgniter. To do this, open the application/config/config.php file and set the $config['enable_hooks'] variable to TRUE.


$config['enable_hooks'] = TRUE;

Step 2: Define Maintenance Config
The next step is to define a custom config variable that will control the maintenance mode. We will use this variable to enable or disable the maintenance mode easily. To do this, add the following code at the end of the application/config/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;

Step 3: Create Maintenance View
The next step is to create a view file that will display the maintenance message. You can design this file as you like, using HTML, CSS, and JavaScript. For simplicity, we will use a basic HTML file with some inline styles. Create a file called maintenance.php in the application/views folder and add the following code.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Site Under Maintenance</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #333;
}

.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}

h1 {
text-align: center;
font-size: 36px;
}

p {
text-align: center;
font-size: 18px;
}
</style>
</head>
<body>
<div class="container">
<h1>Site Under Maintenance</h1>
<p>Sorry for the inconvenience. To improve our services, we have momentarily shutdown our site.</p>
<p>Please try again later.</p>
</div>
</body>
</html>

Step 4: Define Maintenance Hook
The next step is to define a hook that will check the maintenance mode config variable and load the maintenance view file if it is set to TRUE. To do this, open the application/config/hooks.php file and add the following code.


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


This code tells CodeIgniter to execute the offline_check method of the Maintenance_hook class, which is defined in the application/hooks/Maintenance_hook.php file, before the system execution.

Step 5: Create Maintenance Hook Class
The final step is to create the hook class that will implement the offline_check method. Create a file called Maintenance_hook.php in the application/hooks folder and add the following code.


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;
}
}
}
}


This code checks if the config file exists and if the maintenance_mode variable is set to TRUE. If it is, it loads the maintenance view file and exits the application. Otherwise, it does nothing and lets the application run normally.

Testing the Maintenance Mode

Now you can test the maintenance mode by changing the value of the $config['maintenance_mode'] variable in the config file. If you set it to TRUE, you should see the maintenance page when you visit your site. If you set it to FALSE, you should see your normal site.

You can also customize the maintenance page as you like, by adding images, logos, links, countdown timers, or anything else that suits your needs.

Conclusion

In this tutorial, we have learned how to setup maintenance mode in CodeIgniter using hooks. Hooks are a powerful feature of CodeIgniter that allow you to modify the core functionality of the framework without changing the core files. You can use hooks to execute custom code at various points of the application execution.

We have used a pre_system hook to check if the site is in maintenance mode or not, and load a custom view file if it is. We have also defined a custom config variable to control the maintenance mode easily.

Copyright 2024. All rights are reserved