zoukankan      html  css  js  c++  java
  • 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]? 和  方法是不是正在执行的方法   如果是的话执行   钩子中的方法

  • 相关阅读:
    javascript连接SQL Server 2014进行增删改查(适用于IE浏览器)
    javascript连接远程数据库SQL Server 2014(只能在IE浏览器上运行)
    HTML基础:文本列表实例2(9)
    HTML基础:文本列表实例1(8)
    一个简单的例子:javascript实现日期的比较(3)
    一个简单的例子:javascript设置默认日期范围为最近40天(2)
    一个简单的例子:通过javascript输出所选择的日期(1)
    HTML基础:文本列表(7)
    HTML基础:文本的样式标签(6)
    HTML基础:文本的排版格式(5)
  • 原文地址:https://www.cnblogs.com/lijiageng/p/5784995.html
Copyright © 2011-2022 走看看