1. ci背景
ci在这里是一个php框架,官网地址: https://codeigniter.org.cn/
https://codeigniter.org.cn/2. 特点
2.1 键值对object管理
load_class管理对象加载,负责jd生成加载快的对象,存入在键值对中,方便复用。
1 /** 2 * Class registry 3 * 4 * This function acts as a singleton. If the requested class does not 5 * exist it is instantiated and set to a static variable. If it has 6 * previously been instantiated the variable is returned. 7 * 8 * @param string the class name being requested 9 * @param string the directory where the class should be found 10 * @param mixed an optional argument to pass to the class constructor 11 * @return object 12 */ 13 function &load_class($class, $directory = 'libraries', $param = NULL) 14 { 15 static $_classes = array(); 16 17 // Does the class exist? If so, we're done... 18 if (isset($_classes[$class])) 19 { 20 return $_classes[$class]; 21 } 22 23 $name = FALSE; 24 25 // Look for the class first in the local application/libraries folder 26 // then in the native system/libraries folder 27 foreach (array(APPPATH, BASEPATH) as $path) 28 { 29 if (file_exists($path.$directory.'/'.$class.'.php')) 30 { 31 $name = 'CI_'.$class; 32 33 if (class_exists($name, FALSE) === FALSE) 34 { 35 require_once($path.$directory.'/'.$class.'.php'); 36 } 37 38 break; 39 } 40 } 41 42 // Is the request a class extension? If so we load it too 43 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')) 44 { 45 $name = config_item('subclass_prefix').$class; 46 47 if (class_exists($name, FALSE) === FALSE) 48 { 49 require_once(APPPATH.$directory.'/'.$name.'.php'); 50 } 51 } 52 53 // Did we find the class? 54 if ($name === FALSE) 55 { 56 // Note: We use exit() rather than show_error() in order to avoid a 57 // self-referencing loop with the Exceptions class 58 set_status_header(503); 59 echo 'Unable to locate the specified class: '.$class.'.php'; 60 exit(5); // EXIT_UNK_CLASS 61 } 62 63 // Keep track of what we just loaded 64 is_loaded($class); 65 66 $_classes[$class] = isset($param) 67 ? new $name($param) 68 : new $name(); 69 return $_classes[$class]; 70 }View Code
基于这个特点,多次load_class会加载出在其它地方操作过的对象···
3. 自测
以version = 3.1.11为例
3.1 post/get/delete等等自测举例
若在本地进行test验证,需要准备相关全局变量: $_SERVER, $routing
其中$routing, 为ci框架的全局变量
下面是发起一个接口的请求,在cli中实现,需要注意的是在arena项目中,ci框架的入口文件,需要专门编辑出来一个cli文件,可以考虑直接copy所属app运行目录的index.php文件。
1 /** 2 * @brief test func 3 */ 4 //入参准备 5 $_POST = [ 6 "a" => b, 7 ]; 8 //地址准备 9 $_SERVER['CI_ENV'] = "local"; 10 $_SERVER['REQUEST_METHOD'] = "POST"; 11 $routing["directory"] = "api"; 12 $routing["controller"] = "test"; 13 $routing["function"] = "func";
准备完这些后,多数api已经能足够完成自测。
3.2 接口入参注入自测
而ci框架,还支持一种关于入参注入的功能。
举例如下
1 class testN { 2 public function coinInfinite($id) { 3 var_dump($id); 4 } 5 }
即在函数的api层面,支持params的设定,而非通过php兼容的入参协议获得。
通过排查,ci框架的核心类:
arena/src/system/core/URI.php
会操作 $_SERVER全局变量中的元素argv
arena/src/system/core/Router.php
依据ci键值对object的操作,将CI_URI对象,复用在CI_Router中。
CI_Router->_set_routing()由该函数实现
所以根据于此,示例如下
1 /** 2 * @brief test api func 3 */ 4 5 //地址准备 6 $_SERVER['CI_ENV'] = "local"; 7 $_SERVER['REQUEST_METHOD'] = "GET"; 8 array_push($_SERVER['argv'], "api/test/func/123");
argv新插入的数组元素中的123,就是注入到举例函数: coinInfinite中的入参 $id
标签:ci,name,框架,SERVER,directory,php,class From: https://www.cnblogs.com/supermarx/p/18029357