Reputation: 16736
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?
Upvotes: 10
Views: 24245
Reputation: 53
I think this was easy, Just call view on the constructor like below.
comment (site will be live)
class home extends CI_Controller {
public function __construct() {
parent::__construct();
//echo $this->load->view('maintenance','',true);exit;
}
}
uncomment (site will be under maintenance)
class home extends CI_Controller {
public function __construct() {
parent::__construct();
echo $this->load->view('maintenance','',true);exit;
}
}
And you can use your own "maintenance.php" page under "view" in CI Application.
Upvotes: 0
Reputation: 101
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>
Upvotes: 10
Reputation: 11
this one works well,
application/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 importantn don forget to put it at the bottom, it will prevent all pages from being displayed.
application/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;
}
}
}
autoload the library so it will be check every page load.
now you can activate and deactivate maintenance mode by typing or
Upvotes: 0
Reputation: 531
Here my solution, works fine for me:
The below will call maintanance.php immediately, so you can go ahead and break your CI code without the world seeing it.
Also allow you to add you own ip address so that you can still access the site for testing etc.
In index.php add at 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 file Maintanance.php in same folder as index.php (or 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');
?>
Upvotes: 35
Reputation: 2139
Here is what I've come up with for creating a maintenance mode.
<!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 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;
}
}
}
$hook['post_controller'][] = array(
'class' => 'maintenance',
'function' => 'maintenance',
'filename' => 'maintenance.php',
'filepath' => 'hooks',
'params' => array()
);
Upvotes: 3
Reputation: 356
how about this :
auto-loaded libraries can contain something like this :
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'));
}
}
next step is to create a controller for maintenance page.
Upvotes: 2
Reputation: 1577
Extend the CI_Controller by putting a new file in your core directory called MY_Controller.
In this file's constructor, do something like this:
public function __construct()
{
parent::__construct();
if($this->config->item('maintenance_mode') == TRUE) {
$this->load->view('maintenance_view');
die();
}
}
Let all controllers in your app inherit from that class.
Upvotes: 24