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

CI 框架 hooks 的调用方法的更多相关文章

  1. tp框架基础控制器调用方法

    public function indd(){ //调用该控制器下的某个方法 $this ->index(); //跨控制器调用 $k = A("index");// 创建控 ...

  2. CI框架中 日志输出方法log_message()只允许输出字符串解决方案

    1.修改CodeIgniter/index.php,添加如下: define('ROOTDIR',dirname(__FILE__).'/'); 2.修改CodeIgniter/application ...

  3. CI框架之HOOKS使用流程及原理

        Ci框架中Hooks可以理解:在框架的执行流程过程中,允许开发者在固定的某些时间点上(如:调用控制器前,调用控制器后等时间点上),调用其他函数来扩充CI框架执行流程的一种方法.技术上来就是通过 ...

  4. CI框架浅析(全篇)

        业余花了点时间看看CodeIgniter框架(简称CI),CI目前的稳定版本是 3.X,4.0版本已经出来了,但还在测试中,所以我分析的还是 3.x 版本. CI是一个很轻便的框架,整个下载包 ...

  5. CI框架浅析

    CI框架浅析(全篇)     业余花了点时间看看CodeIgniter框架(简称CI),CI目前的稳定版本是 3.X,4.0版本已经出来了,但还在测试中,所以我分析的还是 3.x 版本. CI是一个很 ...

  6. 各种demo——CI框架学习

    各种demo——CI框架学习   寒假学习一下CI框架,请各位多多指教! 一.CI的HelloWorld! 注意:CI禁止直接通过文件目录来访问控制器. ./application/controlle ...

  7. CI框架 -- 核心文件 之 Hooks.php

    钩子 - 扩展框架核心 CodeIgniter 的钩子特性提供了一种方法来修改框架的内部运作流程,而无需修改 核心文件.CodeIgniter 的运行遵循着一个特定的流程,你可以参考这个页面的 应用程 ...

  8. CI框架源码学习笔记5——Hooks.php

    接着Benchmark.php往下看,下一个引入的文件是Hooks.php,我们称之为钩子.它的目的是在不改变核心文件的基础上,来修改框架的内部运作流程.具体使用方法参见手册http://codeig ...

  9. CI框架,源代码一次性判断获取post(get)数据是否有某个字段值为空方法

    一.以下是CI框架 1.把所有的要接收的字段放在数组中 例: 我要接收:id,name,age,mobile 等字段 $req = array('id','name','age','mobile'); ...

随机推荐

  1. Computation expressions: Introduction

    本文仅为对原文的翻译,主要是记录以方便以后随时查看.原文地址为http://fsharpforfunandprofit.com/posts/computation-expressions-intro/ ...

  2. QQ互联功能

    QQ作为现在使用人数最多的几个聊天软件之一,倘若能够方便的进行沟通(在大家的机器上都安装了QQ客户端或者浏览器的前提下),在商家推广的时候也许会带来不小的利益. http://wp.qq.com/in ...

  3. MySQL中整型数据的差别

    bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 个字节. P.S. b ...

  4. OpenCL( 一)

    #include <CL/cl.h> #include <iostream> #include <string> #include <fstream> ...

  5. ajax通过设置Access-Control-Allow-Origin来实现跨域访问

    [在被请求的Response header中加入] // 指定允许其他域名访问(*代表所有域名)header('Access-Control-Allow-Origin:*');// 响应类型heade ...

  6. linux下合并pdf

    使用Gost Script和 PDFtk运行如下命令: #gs -q -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=Linuxidc.pdf -dBATCH *.p ...

  7. php缓存总结

    php缓存技术: 1.全页面静态化缓存;2.页面部分缓存;3.数据缓存;4.查询缓存;5.按内容变更进行缓;6.内存式缓存;7.apache缓存模块;8.php APC缓存扩展;9.Opcode缓存. ...

  8. 缓存HA的开源解决方案

    1) Twitter的Redis/Memcached代理服务:Twemproxy 2) Facebook的Memcached协议路由器:McRouter 3) Youtube的Mysql中间件:Vit ...

  9. Java 基于log4j的日志工具类

    对log4j日志类进行了简单封装,使用该封装类的优势在于以下两点: 1.不必在每个类中去创建对象,直接类名 + 方法即可 2.可以很方便的打印出堆栈信息 package com.tradeplatfo ...

  10. Linux Debian 7部署LEMP(Linux+Nginx+MySQL+PHP)网站环境

    我们在玩VPS搭建网站环境的时候,都经常看到所谓的LAMP.LNMP.LEMP,LAMP, 其中的A代表APECHE WEB驱动环境,LNMP中的N代表NGINX驱动环境,只不过海外的叫法NGINX ...