PHP code example of mzh / hyperf-jwt
1. Go to this page and download the library: Download mzh/hyperf-jwt library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mzh / hyperf-jwt example snippets shell
php bin/hyperf.php jwt:publish --config
shell
namespace App\Controller;
use \Mzh\JwtAuth\Jwt;
class IndexController extends Controller
{
# 模拟登录,获取token
public function login(Jwt $jwt)
{
#用法1 对象模式
$jwtData = new JwtBuilder();
$jwtData->setIssuer('api');
#... 设置更多token属性
#... 设置data数据
$jwtData->setJwtData(['uid' => 123,'type' => 1111,'group' => 1]);
#返回 JwtBuilder对象
$tokenObj = $jwt->createToken($jwtData);
#获取生成的token
$tokenObj->getToken();
#用法2 传入数组
#返回 JwtBuilder对象
$tokenObj = $jwt->createToken(['uid' => $id,'type' => $type,'group' => $group]);
#获取生成的token
$tokenObj->getToken();
#获取刷新token 传入数组 第一个参数为数据,第二个参数为类型,默认是access 可以定义为 refersh 或者其他类型自定义
#返回 JwtBuilder对象
$tokenObj = $jwt->createToken(['uid' => $id,'type' => $type,'group' => $group],Jwt::SCOPE_REFRESH);
#获取生成的token
$tokenObj->getToken();
return $tokenObj->getToken();
}
}