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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With