i am new to codeigniter. In Every interview all asked about hooks. i am not getting that what is hook why i have to use it ? what the benefit of it.
-
Check out this question of Stack Overflow http://stackoverflow.com/q/23805693/3081659 – Noman Aug 29 '16 at 06:45
-
Have you read this http://www.codeigniter.com/user_guide/general/hooks.html – Aug 29 '16 at 06:46
-
i had read that but i am not getting the purpose of hook - syed Noman – Hina Vaja Aug 29 '16 at 06:48
-
Thank you for help – Hina Vaja Aug 29 '16 at 06:51
-
@HinaVaja Its simply like act like `_constructor` in function. `_constructor` will always call under every method used in that controller. In Hooks its load every time when controller initiated. Check this video https://www.youtube.com/watch?v=S6-tPYHW374 – Abdulla Nilam Aug 29 '16 at 06:58
-
Thank you - Spartan – Hina Vaja Aug 29 '16 at 07:03
-
hey, hooks is the thing which allows to alter the core functionality of the system without changing it physically. there are hooks like pre_sytem,post_system,pre_controller,post_controller,etc get executed as they are defined to be. and please do some practicals with below link given http://codeigniter.com/user_guide/general/hooks.html – AmmyTech Aug 29 '16 at 08:05
-
Thanks - Kool-Mind – Hina Vaja Aug 29 '16 at 13:03
2 Answers
Think in hooks as a middleware implementation in codeigniter. Basically you can extend the basic functionallity of the framework adding code to specific moments of the request live cycle.
This is the lists of hooks available in codeigniter 2
The following is a list of available hook points.
pre_system Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.
pre_controller Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.
post_controller_constructor Called immediately after your controller is instantiated, but prior to any method calls happening.
post_controller Called immediately after your controller is fully executed.
display_override Overrides the _display() function, used to send the finalized page to the web browser at the end of system execution. This permits you to use your own display methodology. Note that you will need to reference the CI superobject with $this->CI =& get_instance() and then the finalized data will be available by calling $this->CI->output->get_output()
cache_override Enables you to call your own function instead of the _display_cache() function in the output class. This permits you to use your own cache display mechanism.
post_system Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.

- 1
- 1

- 3,250
- 22
- 37
This is example link for use it hooks in codeigniter
https://qasimbadami.wordpress.com/2012/05/18/codeigniter-hooks-tutorial/
Note : suppose you have big project and almost 100 controller , if every time to check session exists or not , each and every post request so than to use hook

- 838
- 2
- 8
- 24