CodeIgniter Hooks
In CodeIgniter, snares are occasions which can be called when the execution of a program. It permits executing a content with explicit way in the CodeIgniter execution process without adjusting the center records. For instance, it very well may be utilized where you have to check whether a client is signed in or not before the execution of controller. Utilizing snare will spare your time recorded as a hard copy code on numerous occasions.
There are two snare records in CodeIgniter. One is application/config/hooks.php organizer and other is application/snares envelope.
In other language, on the off chance that you need to run a code each time after controller constructor is stacked, you can determine that content way in snares.
Enabling Hooks
To empower Hook, go to application/config/config.php document and set it TRUE as demonstrated as follows.
$config['enable_hooks'] = TRUE;
Defining a Hook
A snare can be characterized in the application/config/hooks.php document. Each snare is characterized as a cluster comprising of the accompanying terms.
$hook['pre_controller'] = cluster(
'class' => 'Classname',
'work' => 'functionname',
'filename' => 'filename.php',
'filepath' => 'snares',
'params' => array('element1', 'element2', 'element3')
);
class - Here, you need to make reference to the name of your group characterized in the hooks.php record. On the off chance that you are utilizing procedural capacity rather than a class, leave it clear.
function- Mention the capacity name you are calling.
filename - The document name made in application/snares organizer and containing class and capacity name referenced previously.
filepath - Here you need to make reference to the name of the catalog which contains your content. Your content must be situated inside the application envelope. On the off chance that your content is situated in application/snares envelope, at that point your way will be just snares. Be that as it may, in the event that your content is situated in application/snares/office envelope, at that point your way will be snares/office.
params - It incorporates the parameters which you need to go in your content and it's discretionary.
Multiple calls to the same Hook
You can utilize cluster multi-dimensional to utilize a similar snare point with more than one content.
$hook['pre_controller'][] = cluster(
'class' => 'Classname1',
'work' => 'functionname1',
'filename' => 'filename1.php',
'filepath' => 'snares',
'params' => array('element1', 'element2', 'element3')
);
$hook['pre_controller'][] = cluster(
'class' => 'Classname2',
'work' => 'functionname2',
'filename' => 'filename2.php',
'filepath' => 'snares',
'params' => array('element4', 'element5', 'element6')
);
Section [] empowers you to have same snare point with various contents. Your execution request will be same as the cluster characterized.
Hook Points
The rundown of snare focuses is demonstrated as follows.
- pre_system
- It is called much before the framework execution. Just benchmark and snare class have been stacked now.
- pre_controller
- It is called promptly preceding your controller being called. Now all the classes, security checks and steering have been finished.
- post_controller_constructo
- It is called following your controller is begun, yet before any technique call.
- post_controller
- It is called following your controller is totally executed.
- display_override
- It is utilized to send the last page toward the finish of document execution.
- cache_override
- It empowers you to call your own capacity in the yield class.
- post_system
- It is called after the last page is sent to the program toward the finish of the framework execution.
Hook Example
1) First of all empower the snare in your CodeIgniter envelope as referenced previously.
2) Create a Controller record example.php in application/controller envelope
<?php
defined('BASEPATH') OR exit('No direct content access permitted');
class Example broadens CI_Controller {
open capacity file()
{
reverberation "CodeIgniter Tutorial";
}
}
On running the above program with URL,
http://localhost/snares/index.php/model, after yield will show up.
3) Create a snares record exm.php in application/snares envelope.
<?php
defined('BASEPATH') OR exit('No direct content access permitted');
class Exm broadens CI_Controller {
open capacity tut()
{
reverberation "Welcome to JavaTpoint. This is ";
}
}
?>
4) Now you need to characterize your snare in the application/config/snares envelope.
<?php
defined('BASEPATH') OR exit('No direct content access permitted');
$hook['pre_controller'] = exhibit(
'class' => 'Exm',
'work' => 'tut',
'filename' => 'exm.php',
'filepath' => 'snares',
);
?>
5) Now again run your program with a similar URL and see the outcome.
???????
