1. Go to this page and download the library: Download l1n6yun/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/ */
use L1n6yun\HyperfJwt\Contracts\JwtSubjectInterface;
use function L1n6yun\HyperfJwt\auth;
// 模型实现了 JwtSubjectInterface 接口
class UserModel implements JwtSubjectInterface{
public function getJwtIdentifier(){
return (string)$this->id;
};
public static function retrieveById($key){
return self::findFromCache($key);
};
}
// 生成token
$userInfo = UserModel::query()->first();
auth()->login($userInfo)
// 退出登陆
auth()->logout();
// 获取载荷
auth()->getPayload();
// 获取用户信息
auth()->user();
// 刷新token
auth()->refresh();
// 检测登陆返回用户ID
auth()->check();
namespace App\Controller;
use L1n6yun\HyperfJwt\Annotation\Auth;
#[Auth] // 全局注解,所有方法都需要验证
class TestController extends AbstractController
{
#[Auth] // 方法注解,该方法需要验证
public function userInfo(){
}
}