Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Codeigniter what is the purpose of hook ? Why we have to create?

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.

like image 477
Hina Vaja Avatar asked Aug 29 '16 06:08

Hina Vaja


People also ask

What is the use of hooks in php?

Hooks are a kind of function which you can plug (or hook) to an existing system to extend its functionality. They aren't specific to the PHP language or to any system. They may also be called plugins, add-ons or extensions.

What is constructor in CodeIgniter?

Constructor is a function that is automatically called when instantiated. this function helps us to intialize the things that we are going to need frequently in our code like when we have to load the models of helpers like form e.t.c.

What is library in CodeIgniter?

Advertisements. The essential part of a CodeIgniter framework is its libraries. It provides a rich set of libraries, which indirectly increase the speed of developing an application. The system library is located at system/libraries. All we need to do is to load the library that we want to use.


2 Answers

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

like image 117
Manish sharma Avatar answered Sep 27 '22 20:09

Manish sharma


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.

like image 40
nicowernli Avatar answered Sep 27 '22 20:09

nicowernli