The CI framework can change or add the core running function of the system without modifying the core file of the system, that is, Hook, to see which hooks of CI: early calls of pre_system system execution. only in benchmark and hooks
The CI framework can change or add the core running function of the system without modifying the core file of the system. Hook is used to check the hooks of the CI:
- Pre_system
Early calls of system Execution. no route or other processes are executed only when the benchmark and hooks classes are loaded.
- Pre_controller
Call before calling any of your controllers. The basic class, route selection, and security check used at this time have been completed.
- Post_controller_constructor
After your controller is instantiated, it is called before any method is called.
- Post_controller
It is called after your controller is fully running.
- Display_override
Overwrite_ Display ()Function to send the final page to the web browser at the end of the system execution. This allows you to display the page in your own way.$ This-> CI = & get_instance ()Reference the CI super object, and then such final data can be called$ This-> CI-> output-> get_output ().
- Cache_override
You can call your own functions to replace_ Display_cache ()Function. This allows you to use your own cache display method.
- Post_system
After the final coloring page is sent to the browser, the browser receives the final data and calls
Hook configuration is simple:
$ Hook ['pre _ controller'] = array ('class' => 'testhook ', // call the class name 'function' => 'test ', // call the function name 'filename' => 'testhook. php ', // file name 'filepath' => 'Hooks', // file or script path, with application as the benchmark 'params' => 'Array () '// specifies the script parameter. optional );
You can also use a two-dimensional array to reference the same mount point multiple times!
/*** Hook, the core running function of the system is changed or added without modifying the core file of the system */class CI_Hooks {/*** to check whether the hook is enabled */var $ enabled = FALSE; /*** config/hooks. the hooks configuration information in php */var $ hooks = array (); // prevents endless loops, because there may also be hooks var $ in_progress = FALSE in the hook program; // construct () {$ this-> _ initialize (); log_message ('debug', "Hooks Class Initialized");}/*** initialization, obtain the hooks combination */function _ initialize () {$ CFG = & load_class ('config', 'core'); // check whether the hook is enabled for the configuration if ($ CFG-> item ('enable _ Hooks') = FALSE) {return;} // checks whether to configure the hook if (defined ('environment ') AND is_file (APPPATH. 'config /'. ENVIRONMENT. '/hooks. php ') {include (APPPATH. 'config /'. ENVIRONMENT. '/hooks. php ');} elseif (is_file (APPPATH. 'config/hooks. php ') {include (APPPATH. 'config/hooks. php ');} if (! Isset ($ hook) OR! Is_array ($ hook) {return;} $ this-> hooks = & $ hook; $ this-> enabled = TRUE;} // hooks/*** run the hook program, the external call is like this: * $ EXT = & load_class ('hooks', 'core'); * $ EXT-> _ call_hook ('pre _ system '); */function _ call_hook ($ which = '') {if (! $ This-> enabled OR! Isset ($ this-> hooks [$ which]) {return FALSE;} // CI supports multiple hooks, the two-dimensional array if (isset ($ this-> hooks [$ which] [0]) AND is_array ($ this-> hooks [$ which] [0]) {foreach ($ this-> hooks [$ which] as $ val) {$ this-> _ run_hook ($ val );}} else {// A hook directly runs the Hook $ this-> _ run_hook ($ this-> hooks [$ which]);} return TRUE ;} // ----------------------------------------------------------------------/*** Run Hook ** Runs a participant hook ** @ accesspriv Ate * @ paramarraythe hook details * @ returnbool */function _ run_hook ($ data) {if (! Is_array ($ data) {return FALSE;} // prevents endless loops, because there may also be hooks in the hook program if ($ this-> in_progress = TRUE) {return ;} // set the path // you can use filepathfilepath to reference the folder (application). under application/hooks, you can use hooks as your filepath if (! Isset ($ data ['filepath']) OR! Isset ($ data ['filename']) {return FALSE;} $ filepath = APPPATH. $ data ['filepath']. '/'. $ data ['filename']; if (! File_exists ($ filepath) {return FALSE;} // ----------------------------------- // Set class/function name/functions $ class = FALSE; $ function = FALSE; $ params = ''; if (isset ($ data ['class']) AND $ data ['class']! = '') {$ Class = $ data ['class'];} if (isset ($ data ['function']) {$ function = $ data ['function'];} if (isset ($ data ['params']) {$ params = $ data ['params'];} if ($ class === false and $ function === FALSE) {return FALSE;} // you can leave it empty. $ this-> in_progress = TRUE; // after obtaining the hook configuration information, run the hook program if ($ class! = FALSE) {if (! Class_exists ($ class) {require ($ filepath);} $ HOOK = new $ class; $ HOOK-> $ function ($ params);} else {if (! Function_exists ($ function) {require ($ filepath) ;}$ function ($ params) ;}/// after the corresponding program is executed, change the current hook status to non-running so that it can be triggered again. $ This-> in_progress = FALSE; return TRUE ;}}