Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build in 'maintenance mode' in Codeigniter?

I'm using latest codeigniter and I need to create a flag (ideally in the config) when turned to 'true', all pages display a 'maintenance mode' message instead of executing their controller code.

What is the best/simplest practice for doing this?

like image 457
David Avatar asked Mar 22 '13 14:03

David


1 Answers

Here my solution, simply, clean and effectively for all urls calls and SEO respects:


Add this variables in: 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 conditional at the end of: 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 maintenance in: application/controllers/your_controller.php

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

Create view: application/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>
like image 180
xavi papi Avatar answered Oct 08 '22 19:10

xavi papi