Creating Hooks in CodeIgniter

There you are, quite happily using some framework or CMS to run a website, and you've just been asked to get it to do something which it was not originally designed. You begrudgingly agreed and start to work out how to implement what's been asked. You decide that you'll amend the core system files of whatever platform you've been stuck with because it's quick and easy, and because your boss used terms such as "quick wins" and "cost efficient".

Anyway, the requested functionality works and everyone's happy—except the poor person (that may still be you) who comes along a few months later to upgrade the platform to the latest version and…BANG! That amendment you made to the system core, forget it! It's been lost by overwriting it with the new files from the upgrade. Now, that crazy thing you were asked to implement no longer works and you're left trying to remember what you did to get it working. Then before anyone notices, it's missing, and you're left thinking. "oh God why can't I just go home!"

Well cheer up! You're using CodeIgniter and that should never happen to you (providing you use Hooks of course). You can still hack the core if you wish—go ahead, hack away, see if I care—but you'll kick yourself when you have to upgrade. I'm only thinking about your stress levels.

Hooks allow you to override the behavior of CodeIgniter at specific points at its execution without the need to make changes to the CodeIgniter core files. Fantastic! That means that with CodeIgniter, you can implement any number of ludicrous management change requests and it won't affect you on upgrade day one bit. Hooks work in a specific order. What does that mean? Well, it means that CodeIgniter works in a specific order, that is to say, when CodeIgniter runs, it loads specific parts of itself in a set order. This order is always the same, and you can set a Hook to execute at any one of the steps.

Getting ready

First, you need to tell CodeIgniter that it should allow Hooks to run. Open the /application/config/config.php file and ensure that the setting for enable_hooks is TRUE, as shown in the following code snippet:

$config['enable_hooks'] = TRUE;

You'll also have to decide the best time for your Hook to run. There are seven points in the execution of CodeIgniter where you can set your Hook to run, as follows:

  • pre_system: This is the earliest place you can set a Hook to run. Only Benchmarking and Hooks are brought into play at this stage
  • pre_controller: This is executed before any of your controllers are called
  • post_controller_constructor: This makes your Hooks run after a controller constructor but before any controller functions are called
  • post_controller: This makes your Hook run after the controller has finished executing
  • display_override: This will override the CodeIgniter display() function--this is when CodeIgniter tried to render view files or other output to screen
  • cache_override: This overrides the _display_cache() function,you can use this if you wanted to implement custom caching functionality
  • post_system: This is to be called once the normal operation has finished (that is, after the system has finished its execution of the current request)

How to do it...

  1. Define an execution point for your hook. Once you've decided the point to run your Hook, you'll need to tell CodeIgniter when to run the hook. You do this, by defining the correct information in the $hook array in the config/hooks.php file.

    The key of the $hook array specifies the execution point --that is when you want the hook to run (see the list mentioned in the Getting ready section of this recipe). In the following example, the array key is post_controller, which means that the hook is executed after the controller has finished executing. The $hook array now looks like the following.

    $hook['post_controller'] = array(
        'class'    => 'Class_name',
        'function' => 'function_name',
        'filename' => 'file_name_of_hook.php',
        'filepath' => 'path_to_hook',
        'params'   => array(param1, param2, param3 ... etc)
        );

    So what does all the preceding code mean then? Let' have a look at the following table to understand the preceding code better:

    Array element

    Description

    class

    It is the name of the class, which is in the file defined in the filename element.

    function

    It is the name of the function in the class.

    filename

    It is the name of the file which contains the class.

    filepath

    This is the location of the file defined in the array element filename, normally the folder hooks, but you can add subfolders or move it to a different location if you wish.

    params

    They are any arguments you wish to pass to the function element of your Hook class. Separate each argument by a comma.

  2. You then need to create your hook class, as shown in the following code snippet:
    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'),
    
    class Class_name {
      function function_name () {
      }
    }

    You put the code for your hook in this class, obviously change Class_name to something more useful for you, and the function function_name() is obviously an example--change this name too.

Should you need to gain access to CodeIgniter resources in your hook, you can do so by accessing the main CodeIgniter object, using the CodeIgniter get_instance() function shown in the following code:

function function_name () {
  $CI = & get_instance();
  $CI->load->thing_to_load();
}

Here, thing_to_load is the name of your model, or library, and so on. There you have it--hooks are simple really, decide on an execution point, create a class to contain the code for your hook, and away you go!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.6.194