1. Go to this page and download the library: Download wiseadmin/wise-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/ */
// route/api.php
use wise\jwt\middleware\JwtAuth;
Route::group('api', function () {
Route::get('profile', 'api.User/profile');
Route::post('order', 'api.Order/create');
})->middleware(JwtAuth::class);
class UserController
{
public function profile()
{
// 在控制器中获取 payload 的两种方式
$payload = request()->jwtPayload;
// 或
$userId = request()->jwtPayload['sub'];
}
}
// 在配置中定义提取器链
// 优先从 Header 提取,其次 Cookie,最后 Query
use wise\jwt\extractor\ChainExtractor;
use wise\jwt\extractor\BearerHeaderExtractor;
use wise\jwt\extractor\CookieExtractor;
use wise\jwt\extractor\QueryExtractor;
$extractor = new ChainExtractor([
new BearerHeaderExtractor(),
new CookieExtractor('access_token'),
new QueryExtractor('token'),
]);