【问题标题】:Codeigniter, how do I use jwt library from third party folder in conjunction with models?Codeigniter,如何将第三方文件夹中的 jwt 库与模型结合使用?
【发布时间】:2017-05-07 01:45:43
【问题描述】:

我遇到了一个难题。

我已经使用 composer 在我的第三方文件夹中安装了 https://github.com/firebase/php-jwt 这个库。

如何使用带有一些参数的模型或控制器中的JWT::encode

例如,我想从控制器调用 getToken($parameters) 模型,然后将该模型加载到该 JWT 库中。我已经用谷歌搜索了很多,但似乎我的 PHP 知识有限。

【问题讨论】:

    标签: php codeigniter jwt


    【解决方案1】:

    好的,解决了。

    您所做的是安装第三方库。 将其自动加载到库文件夹中的新文件中,如下所示。

    <?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    {
            require_once(APPPATH . 'third_party/firebase-jwt/vendor/autoload.php');
            require_once(APPPATH . 'third_party/firebase-jwt/start.php');
    
    }
    class Firebasetoken { 
       function __construct($permissions = false) {
    
        $this->codeigniter_instance =& get_instance();
        $this->theToken = buildToken($permissions);
    }
    
    function get_firebase_token(){
        return $this->theToken;
    }
    

    在你的 start.php 中像这样加载,因为命名空间和 CI 显然不能很好地发挥作用。

      <?php
    
        require_once 'jwt/src/BeforeValidException.php';
        require_once 'jwt/src/ExpiredException.php';
        require_once 'jwt/src/SignatureInvalidException.php';
        require_once 'jwt/src/JWT.php';
    
        use Firebase\JWT\JWT;
        function buildToken($permissions){
            //add example from jwt lib in here
        }
    

    然后这样称呼它。

    $this->load->library('FirebaseToken', $permissions);
    $token = $this->firebasetoken->get_firebase_token();
    

    瞧。 希望它能节省一些时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 2019-10-19
      • 2020-04-19
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多