0 Comments   Friday, 9th October 2009

Playing with Hooks in CodeIgniter

Since starting with CodeIgniter, I had been looking for a way to put any websites I created in maintenance mode.  I had considered a couple of options.  The first being to replace main index.php file with another that contained the maintenance info.  The second was to add some lines of code at the top of the index.php file.  Basically, I'd add these lines of code at the top of the index.php file

$site_offline = TRUE;
if ($site_offline) {
 include('offline.php');
die();
}

An offline.php file will have to be created at the root of my application.

Both aforementioned methods will work.  My only bugbear with this is that everything has to be done manually.  When designing a dynamic application, I want my customers to be able to log into the back end of the website and just flip a switch to put the site into offline mode instead of constantly having to edit the config files themselves.

After having a good discussion on this with a friend of mine, I opted to use CodeIgniter's Hooking system.  CodeIgniter's user guide gave some example scenarios where hooking into CI's core (without hacking) would allow me run scripts at specific hook points in my applications.  Since I wanted to use a database to achieve what I wanted, this method was right up my alley.

To enable the use of hooks in my application, I had make a change in my config file.  I had to set $config['enable_hooks'] to TRUE.  By doing this, the hook feature was enabled globally across my application.

Next thing to do was to create a settings table in my database:

CREATE TABLE `settings` (
  `id` int(11) DEFAULT NULL,
  `site_offline` tinyint(4) DEFAULT '0',
  `offline_reason` text CHARACTER SET utf8
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

I then created an offline.php file within my application.  I used the offline.php file to connect to the database and retrieve and display the reason why my application would be offline.

<?php
include(APPPATH.'config/database'.EXT);
$connection = mysqli_connect($db['default']['hostname'], $db['default']['username'], $db['default']['password']) or die ("couldn't connect with Database");
$connection->select_db($db['default']['database']) or die("Could not select database.  Please review your settings!");
$result = $connection->query('SELECT * FROM settings');
$settings = $result->fetch_object();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Site Offline</title>
</head>

<body>
<h1>Site Is Offline</h1>
<p><?php echo $settings->offline_reason; ?></p>
</body>
</html>
<?php
$connection->close();
?>

In order to avoid having to re-define my database settings, I simply included the database.php file from my application's config folder.  Hence the line:

include(APPPATH.'config/database'.EXT);

Once I had included the database file, it was a simply a matter of connecting to the database and retrieve the required information from the settings table.

$connection = mysqli_connect($db['default']['hostname'], $db['default']['username'], $db['default']['password']) or die ("couldn't connect with Database");
$connection->select_db($db['default']['database']) or die("Could not select database.  Please review your settings!");
$result = $connection->query('SELECT * FROM settings');
$settings = $result->fetch_object();

Next thing I did was to create a file called system_check.php in my application's hooks folder.  The contents of the system_check.php file are as follows:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class System_Check
{
 function check_site_status()
 {
  $CI =& get_instance();

  $query = $CI->db->query('SELECT * FROM settings');
  
  if ($query->num_rows > 0) {
   $settings = $query->row();
  }

  $site_offline = $settings->site_offline;
  
  if ($site_offline) {
   include(APPPATH.'offline.php');
   die();
  } else {
   return false;
  }  
 }
}

?>

To finish things off, I had to define my hook in the hooks file located in my application's config folder. My hook was defined as follows:

$hook['post_controller_constructor'] = array(

    'class' => 'System_Check',
    
    'function' => 'check_site_status',
    
    'filename' => 'system_check.php',
    
    'filepath' => 'hooks'

);

 In order to test if everything was work, all I had to do was change the value of the site_offline in my database to 1 and add a reason.  If I tried to view any pages on my application, my offline page would be loaded displaying the reason why my application is offline.

I hope this helps anyone who is looking to setup something similar.

Let me know your thoughts.

 

 

 

 
Filed In: CodeIgniter
Share |


« Previous Article Next Article »



Other Posts You Might Like


blog comments powered by Disqus

Archives

 
 

Latest Tweets

Recent Comments

Powered by Disqus

 


About Me

My name is Emmanuel Okorie. I am by profession a web designer who is trying to expand his expertise into other areas of media such as Digital Video, Photography and General Design.

You can read a bit more about me by visiting the About Me page.

 

 
[ Login ]