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]? 和  方法是不是正在執行的方法   如果是的話執行   鈎子中的方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM