In CodeIgniter, hooks are events which can be called before and after the execution of a program. It allows executing a script with specific path in the CodeIgniter execution process without modifying the core files. For example, it can be used where you need to check whether a user is logged in or not before the execution of controller. Using hook will save your time in writing code multiple times.
There are two hook files in CodeIgniter. One is application/config/hooks.php folder and other is application /hooks folder.
In other language, if you want to run a code every time after controller constructor is loaded, you can specify that script path in hooks.
To enable Hook, go to application/config/config.php file and set it TRUE as shown below.
Defining a Hook
A hook can be defined in the application/config/hooks.php file. Each hook is defined as an array consisting of the following terms.
class - Here, you have to mention the name of your class defined in the hooks.php file. If you are using procedural function instead of a class, leave it blank.
function - Mention the function name you are calling.
filename - The file name created in application/hooks folder and containing class and function name mentioned above.
filepath - Here you have to mention the name of the directory which contains your script. Your script must be located inside the application folder. If your script is located in application/hooks folder, then your path will be simply hooks. But if your script is located in application/hooks/office folder, then your path will be hooks/office.
params - It includes the parameters which you want to pass in your script and it's optional.
Multiple calls to the same Hook
You can use array multi-dimensional to use the same hook point with more than one script.
Bracket  enables you to have same hook point with multiple scripts. Your execution order will be same as the array defined.
The list of hook points is shown below.
It is called much before the system execution. Only benchmark and hook class have been loaded at this point.
It is called immediately prior to your controller being called. At this point all the classes, security checks and routing have been done.
It is called immediately after your controller is started, but before any method call.
It is called immediately after your controller is completely executed.
It is used to send the final page at the end of file execution.
It enables you to call your own function in the output class.
It is called after the final page is sent to the browser at the end of the system execution.
1) First of all enable the hook in your CodeIgniter folder as mentioned above.
2) Create a Controller file example.php in application/controller folder
On running the above program with URL,
http://localhost/hooks/index.php/example, following output will appear.
3) Create a hooks file exm.php in application/hooks folder.
4) Now you have to define your hook in the application/config/hooks folder.
5) Now again run your program with the same URL and see the result.
Next TopicPassing Parameters in CodeIgniter