This article explains the CI framework source code interpretation and uses the Hook.php file to complete function expansion. . Share it with everyone for your reference, the details are as follows:
After reading the source code of hook.php, you will know the principle of CI using hooks to expand.
Basic knowledge of hooks http://codeigniter.org.cn/user_guide/general/hooks.html
The use of hooks in CI goes through a process: opening the hook, defining the hook, calling the hook, and executing the hook.
The manual has already informed the methods of opening, defining and calling. So what is the implementation principle of hook?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class CI_Hooks { var $enabled = FALSE; //开启hook的标志,默认是关闭的。APPPATH/config/config.php中的配置也是默认关闭的,如果想使用hook,要在config.php中开启。 var $hooks = array(); //在_initialize()函数初始化的过程中将APPPATH/config/hook.php中定义的hook数组,引用到$this->hooks; var $in_progress = FALSE; //当一个hook执行的时候,会给标记 $in_process = TRUE ,是为了防止同一个hook被同时调用。 function __construct() { $this->_initialize(); log_message('debug', "Hooks Class Initialized"); } function _initialize() { //初始化hook //判断config.php中是否开启hook //include(hook.php),将文件里定义的hook数组引用到$this->hooks //$this->enable = TRUE } function _call_hook($which = '')//pre_system { //以pre_system挂钩点为例,当调用_call_hook('pre_system')时 //确保$this->enable = TRUE && 定义了$this->hooks['pre_system'] //如果是二维数组就遍历,依次_run_hook($this->hooks['pre_system'][$val]) //如果是一维数组,那么直接_run_hook($this->hooks['pre_system']) } function _run_hook($data) //$data 是传递过来的hook数组 { //$data 就是我们在APPPATH/config/hook.php 定义的hook数组 //$hook['pre_controller'] = array( // 'class' => 'MyClass', // 'function' => 'Myfunction', // 'filename' => 'Myclass.php', // 'filepath' => 'hooks', // 'params' => array('beer', 'wine', 'snacks') // ); //取出data里面的数据,加载 APPPATH.$data['filepath'].$data['filename']; //实例化钩子类,调用function。应用到示例中就是 $this->in_process = TRUE; $Hook = new MyClass(); $Hook->Myfunction($params); $this->in_process = FALSE; } } ?>
The hook point can hang multiple hooks, so when we want to expand ci, we only need to put the hook file in the APPPATH folder, and then declare the defined hook information in APPPATH/config/hook.php. Can. Then when the system reaches the hook point, it will automatically call the declared hook.
This enables scalability
Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "Summary of Common Methods in ThinkPHP", "Introduction Tutorial on Zend FrameWork Framework", "Introduction Tutorial on PHP Object-Oriented Programming", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.