2015-04-17 126 views
0

我用作曲器從https://github.com/firebase/php-jwt安裝了php-jwt。帶有Zend 2.0的Firebase PHP-JWT無法加載模塊

我現在有了位於vendor \ firebase \ php-jwt \ Firebase \ PHP-JWT的庫,該目錄的內容與git上的相同。

在application.config.php

我有:

'modules' => array(
    // 'ZendDeveloperTools', 
    'DoctrineModule', 
    'DoctrineORMModule',   
    'JWT' 
), 

在autoload_namespaces.php我有:

'JWT\\' => array($vendorDir . '/firebase/php-jwt/Firebase'), 

autoload_classmap.php:

'JWT' => $vendorDir . '/firebase/php-jwt/Firebase/PHP-JWT/Authentication/JWT.php', 

錯誤:

Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (JWT) could not be initialized.' in \vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php on line 175 

我不知道我做錯了什麼。

回答

1

php-jwt lib不是ZF2模塊,因此無法將其加載爲zf2模塊。使用時只需將JWT LIB你的模塊(從ZF2骨架的應用程序如應用模塊)在

+0

對我來說,我已經做到了,謝謝你的回答。我是zend的新手,很棘手,因爲作曲家和其他模塊一起安裝了它。 – Alex7

2

我們可以從AuthController如同直接使用JWT:

在頂部:

use Firebase\JWT\JWT; 

然後創建一個功能生成JWT令牌,如:

public function generateAuthToken($userData, $data) { 
     if(password_verify($data['password'], $userData['hashPassword'])) { 
      $tokenId = base64_encode(mcrypt_create_iv(32)); 
      $issuedAt = time(); 

      $data = [ 
       'iat' => $issuedAt,   // Issued at: time when the token was generated 
       'jti' => $tokenId,   // Json Token Id: an unique identifier for the token 
       'data' => $userData 
      ]; 

      $jwt = JWT::encode($data, 'testKey', self::JWT_ALGO); //use self::JWT_ALGO => HS512 
      $unencodedArray = ['jwt' => $jwt]; 
      return json_encode($unencodedArray); 
     } 
    } 

而且使用下面的函數來檢查和解碼令牌:

public function checkAuthToken($data) { 
     $request = $this->getRequest(); 
     $authHeader = $request->getHeader('Authorization'); 

     if($authHeader) { 
      list($token) = sscanf($authHeader->toString(), 'Authorization: Bearer %s'); 
      if($token) { 
       try{ 
        $secretKey = 'testKey'; 
        $decryptedToken = JWT::decode($token, $secretKey, [self::JWT_ALGO]); // //use self::JWT_ALGO => HS512 
        return $decryptedToken->data; 
       } catch (\Exception $ex) { 
        return false; 
       }     
      } 
     } 
     return false; 
    }