How to build a “maintenance mode” in Codeigniter?

I use the latest codeigniter, and I need to create a flag (ideally in the configuration), when it is turned to “true”, all pages display the message “maintenance mode” instead of executing its controller code.

What is the best / easiest practice for this?

+6
source share
6 answers

Extend CI_Controller by placing the new file in your main MY_Controller directory.

In this file constructor, do the following:

public function __construct() { parent::__construct(); if($this->config->item('maintenance_mode') == TRUE) { $this->load->view('maintenance_view'); die(); } } 

Let all the controllers in your application inherit from this class.

+21
source

Here is my solution, works fine for me:

Maintanance.php will be called below immediately, so you can continue and break your CI code if the world does not see it.

You can also add your own IP address so that you can access the site for testing, etc.

In index.php add on top:

 $maintenance = false; ## set to true to enable if ($maintenance) { if (isset( $_SERVER['REMOTE_ADDR']) and $_SERVER['REMOTE_ADDR'] == 'your_ip') { ##do nothing } else { error_reporting(E_ALL); ini_set('display_errors', 1); ## to debug your maintenance view require_once 'maintenance.php'; ## call view return; exit(); } } 

Add the Maintanance.php file to the same folder as index.php (or the update path above):

 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Maintenance</title> <style> body { width:500px; margin:0 auto; text-align: center; color:blue; } </style> </head> <body> <img src="images/home_page_logo.png"> <h1><p>Sorry for the inconvenience while we are upgrading. </p> <p>Please revisit shortly</p> </h1> <div></div> <img src="images/under-maintenance.gif" > </body> </html> <?php header('HTTP/1.1 503 Service Temporarily Unavailable'); header('Status: 503 Service Temporarily Unavailable'); header('Retry-After: 3600'); ?> 
+26
source

Here's my solution, simple, clean and efficient for all URL requests and SEO respects:


Add these variables to: application /Config/config.php

 $config['maintenance_mode'] = TRUE; $config['maintenance_ips'] = array('0.0.0.0', '1.1.1.1', '2.2.2.2'); 

Add this condition at the end: application /Config/routes.php

 if(!in_array($_SERVER['REMOTE_ADDR'], $this->config->item('maintenance_ips')) && $this->config->item('maintenance_mode')) { $route['default_controller'] = "your_controller/maintenance"; $route['(:any)'] = "your_controller/maintenance";"; } 

Create method support in: application / controllers / your_controller.php

 function maintenance() { $this->output->set_status_header('503'); $this->load->view('maintenance_view'); } 

Create view: app / views / maintenance_view.php

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

how about this:

  • Create automatically loaded libraries that always check the maintenance flag in your database.
  • create a module to manage the service flag of your application.
  • create a module for redirection when service mode is enabled

automatically loaded libraries may contain something like the following:

 class Maintenance_mode { function __construct(){ $CI =& get_instance(); $check_maintenance = $CI->db->select('flag_mode')->get('tbl_settings')->result(); if($check_maintenance[0]->flag_mode == '1') redirect(site_url('maintenance_mode_controller')); } } 

The next step is to create a controller for the maintenance page.

+2
source

Here is what I created to create a maintenance mode.

  • Include "Hooks" in config.php file
  • Create the error_maintenance.php page in the errors folder
  • Create a binding called
  • In the hooks configuration settings, your hooks call runs on post_controller

application / errors / error_maintenance.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> 

app / 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->load->config("config_maintenance"); if(config_item("maintenance")) { $_error =& load_class('Exceptions', 'core'); echo $_error->show_error("", "", 'error_maintenance', 200); exit; } } } 

application /Config/hooks.php

 $hook['post_controller'][] = array( 'class' => 'maintenance', 'function' => 'maintenance', 'filename' => 'maintenance.php', 'filepath' => 'hooks', 'params' => array() ); 
+2
source

it works well

app / views / vw_maintenance.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> <?php exit(); ?> 

The exit () function is very important so as not to forget to put it at the bottom, this will prevent all pages from being displayed.

applications / libraries / maintenance.php

 class Maintenance{ private $CI; public function __construct() { $this->CI =& get_instance(); // flag on and off $this->flag( $this->CI->uri->segment(1) ); // get the flag status $check_maintenance = $this->CI->db->select('flag_mode')->where(array('setting_name' => 'maintenance'))->get('tbl_settings')->result(); //display view if true if($check_maintenance[0]->flag_mode == '1') $this->CI->load->view('vw_maintenance'); } private function flag($command){ $this->CI->db->where('setting_name', 'evolving'); switch($command){ case "activate": $this->CI->db->update('tbl_settings', array('flag_mode' => 1) ); break; case "deactivate": $this->CI->db->update('tbl_settings', array('flag_mode' => 0) ); redirect(site_url('/')); break; } } } 

load library to check loading of each page.

Now you can activate and deactivate the service mode by typing or

0
source

All Articles