CI 框架 hooks 的调用方法
流程:在hooks中写一个类 , 在system/core/CodeIgniter.php 判断什么时候执行 hooks中的类 涉及到了php反射获取类 方法 方法中的注释
钩子的介绍 :
启用 钩子
定义钩子
例子:hooks tokenverify.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of tokenverify
*
* @author root
*/
class TokenVerify {
// Codeigniter instance
protected $_ci;
// Instance of this class
public static $instance;
// Action statics
public static $actions;
public static $current_action;
public static $run_actions;
// Plugins
public static $plugins_pool;
public static $plugins_active;
// Directory
public $plugins_dir;
// Error and Message Pools
public static $errors;
public static $messages;
public function __construct($params = array()) {
// Codeigniter instance
$this->_ci = & get_instance();
$this->_ci->load->database();
}
/**
* Instance
* The instance of this plugin class
*
*/
public static function instance() {
if (!self::$instance) {
self::$instance = new TokenVerify();
}
return self::$instance;
}
/*
* 检测用户端token值是否合法,并转换为用户信息
* @param $token string token的值
* */
public function UserTokenVerify() {
$token = $this->_ci->input->post_get('token', FALSE);
if (empty($token)) {
show_error('token is error');
} else {
$token = base64_decode($token);
$this->_ci->load->model('User_model');
$user_data = $this->_ci->User_model->check_token($token);
// d($user_data);
if ($user_data) {
$this->_ci->user_id = $user_data->user_id;
} else {
show_error('token is error');
}
}
}
/*
* 检测医生端token值是否合法,并转换为用户信息
* @param $token string token的值
* */
public function DoctorTokenVerify() {
$token = $this->_ci->input->post_get('token', FALSE);
if (empty($token)) {
show_error('token is error');
} else {
$token = base64_decode($token);
$this->_ci->load->model('Doctor_model');
$doctor_data = $this->_ci->Doctor_model->check_token($token);
if ($doctor_data) {
$this->_ci->doctor_id = $doctor_data->user_id;
} else {
show_error('token is error');
}
}
}
}
config/hooks.php
$hook['UserTokenVerify'] = array(//用户token验证
'class' => 'TokenVerify',
'function' => 'UserTokenVerify',
'filename' => 'tokenverify.php',
'filepath' => 'hooks'
);
$hook['DoctorTokenVerify'] = array(//医生token验证
'class' => 'TokenVerify',
'function' => 'DoctorTokenVerify',
'filename' => 'tokenverify.php',
'filepath' => 'hooks'
);
/*
* ------------------------------------------------------
* Is there a "pre_controller" hook?
* ------------------------------------------------------
*/
$EXT->call_hook('pre_controller');
/*
* ------------------------------------------------------
* Instantiate the requested controller
* ------------------------------------------------------
*/
// Mark a start point so we can benchmark the controller
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
$CI = new $class();
$ref_class = new ReflectionClass($class);//建立这个类的反射类
$methods = $ref_class->getMethods();//获取所有的方法
foreach($methods as $function){
$doc=$function->getDocComment();//获取注释
$ref_method=$function->getName();
//$CI->router->fetch_method(); 正在执行的方法
if(strpos($doc,'[UserTokenVerify]')&&$ref_method===$CI->router->fetch_method()){
$EXT->call_hook('UserTokenVerify');
}
if(strpos($doc,'[DoctorTokenVerify]')&&$ref_method===$CI->router->fetch_method()){
$EXT->call_hook('DoctorTokenVerify');
}
}
解释 首先看看 注释里有[UserTokenVerify]? 和 方法是不是正在执行的方法 如果是的话执行 钩子中的方法
最新文章
- 动态加载JS 和 CSS
- 关于如何显示Jianshu图片的方案
- MySQL5.6安装步骤
- 如何让Ue4画面产生振动效果
- Cach&#233;数据库学习笔记(2)
- SSIS 项目部署模型
- 【fedora】制作安装u盘
- J2EE 第二阶段项目之分析业务
- String字符串包含运算符实现运算
- OpenStack: OVS安装
- js jquery 判断IE有效方法
- SGU326Perspective(网络流量的最大流量)(经典赛车模型)
- ASP.NET MVC 5– 采用Wijmo MVC 5模板1创建应用程序分钟
- PCI源码学习笔记
- Android开发——Toast知识
- 转:java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)
- Algorithm -->; 求N以内的真分数个数
- Python中迭代输出(index,value)的几种方法
- 构建一个maven聚合类型的横向可扩展项目
- HBase scan 时 异常 ScannerTimeoutException 解决