您的位置:首页 > 其它

CI 框架 hooks 的调用方法

2016-08-18 18:27 281 查看

流程:在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]? 和  方法是不是正在执行的方法   如果是的话执行   钩子中的方法

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: