Friday, 30 May 2014

How to Enable/Use Hooks in codeigniter the codeigniter hooks

15:41

Codeigniter Hooks

Today I am going to show/Teach you how to modify/Tap into the inner working of your codeigniter framework. When Codeigniter Runs it follows a specific Execution process Described in a pretty good detail below in the Diagram.
The upper diagram Shows the flow of data through the whole System in Codeigniter.
1.  At first glance the Index.php serves as a front controller and contain all the basic resources which are required by Codeigniter to run properly.
2.  After that the Router Examines the HTTP request in order to determine what action to take on this incomming request.
3. If the exists the cache file then the router send it directly to the browser which requested the content ,getting around the normal Execution of the system.
4. Here comes Security the most Important aspect, before loading the controller the HTTP request and the data submitted by any user is filtered for security.
5. In this step Controller loads these couple of things core libraries, model, helpers, and any other resources that are specified by the developer or other built in resources required to process the specific request.

6. Finally the View is rendered or the contetn is rendered which you have created in side the directory named views then sent to the web browser (Note:This browser is the browser of end user whi is requesting for the webpage) to be seen. If caching is enabled, the view is cached first so that on subsequent requests it can be served.
Here we Done!
For Now lets move to deploy Codeigniter hooks.
You might be thinking that i am going to teach you a hacking tip then you are absolutely wrong basically Codeigniter provides some cool ways to change its inner functionality without hacking its core functionality by using codeigniter hooks.
So Lets have a deep look:

Step 1:

Frst Enable the hook feature of codeigniter by setting the following item in this Config.php file:
application/config/config.php
In the Config.php file look Search for this line of Code:
By default it is FALSE looks like this: 

$config['enable_hooks'] = FALSE; 


Write TRUE instead of FALSE After Enabling it looks like this:

$config['enable_hooks'] = TRUE;

Step 2:

After Enabling the hook feature of Codeigniter define your HOOK in  application/config/hooks.php file.
Each hook is specified as an array with Its prototypes:
before this Create a directry with your own name inside Application directory in my case i have created a directory named hooks in this directory place your class which you wish to load.
Note: This Class includes the scripts that you want to load. 
For Example in my case this is my hook you can also define Multiple Hooks but for Now I am going to define Just One Hook.
$hook['pre_controller'] = array(
                                ‘class’    => ‘MyClass’,
                                ‘function’ => ‘MyFunction’,
                                ‘filename’ => ‘Myclass.php’,
                                ‘filepath’ => ‘hooks’,
                                ‘params’   => array(‘beer’, ‘wine’, ‘snacks’)
                                );
Useful Note:
The array index correlates to the name of the particular hook point you want to use. In the above example the hook point is pre_controller. A Detailed description list of hook points is found in the section below. The following items should be defined in your associative hook array:
  • Class  The name of the class you wish to invoke placed inside the hooks directory Note: Hook directory is that directory which you have created spetially for hooks insite Application directory. If you wish to use your procedural function in step of a Class,  In this case leave this item blank/Empty.
  • Function  name of the function you wish to call (Note: It resides in the directory ehich you have created inside the application directory of Codeigniter).
  • Filename  The file name containing your class/function(Note: This file is your file you have created inside the hooks directory in this example it is hookprac.php).
  • Filepath  The name of the directory which contains your Script file(That file contains your functions taht you want to execute when hooks runs).

(Note: Your script must be located in a directory INSIDE your application folder, so the file path is relative to that folder. For example, if your script is located in application/hooks, you will simply use hooks as your filepath. If your script is located in application/hooks/utilities you will use hooks/utilities as your filepath. No trailing slash.)
  • Params(Optioanl Feature)  Params are the parameters that you wish to pass to your Script these parameters are defined by yourself/Custom defined.
You can also Change the Codeigniter hooks Points:
Hook points are the parameters of hook that defines before loading which part of the Controller you want to Run your Script. 
Here shown some hook Points:
pre_controller
pre_system
post_controller_constructor
post_controller
display_override
cache_override
post_system

Step 3:

Now Point your browser to your contoller in order to testify codeigniter hooks, if there is no mistake your script/code will run before the controller loads.
You have all done!
If you Have Questions in mind Then You are free to ask in the comment Section Below.

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 Elightener . All rights resevered. Designed by Templateism

Back To Top