在我的 Laravel 7 后端中,我有一些 api url,例如:
Route::group(['namespace' => 'Api', 'middleware' => ['auth:sanctum']], function(){
Route::post('logout/all', 'Auth\LoginController@logoutAll');
Route::post('logout', 'Auth\LoginController@logout');
Route::put('profile/{profile}', 'ProfileController@update');
});
路由 /logout 和 /profile/1 工作正常,而路由 /logout/all 给出错误:
{
"error": "Unauthenticated"
}
我使用的令牌是正确的,因为我可以将它用于其他路线并且它有效。
当然,在尝试 /logout/all 之前我没有调用 /logout。
我尝试更改 logoutAll 的控制器功能,将其设置为“注销”:
Route::group(['namespace' => 'Api', 'middleware' => ['auth:sanctum']], function(){
Route::post('logout/all', 'Auth\LoginController@logout');
Route::post('logout', 'Auth\LoginController@logout');
Route::put('profile/{profile}', 'ProfileController@update');
});
它以这种方式工作,但这会调用相同的函数。
这是函数的代码:
public function logout(Request $request)
{
Auth::user()->tokens()->where('id', Auth::user()->currentAccessToken()->id)->delete();
return response()->json(['data' => 'User logged out.'], 200);
}
public function logoutAll(Request $request)
{
//do nothing
return response()->json(['data' => 'User loggedAll out.'], 200);
}
请帮帮我。
更新
如果我使用 CURL 命令,这是输出:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The POST method is not supported for this route. Supported methods: GET, HEAD. in file /Applications/MAMP/htdocs/test-server/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php on line 117
#0 /Applications/MAMP/htdocs/test-server/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php(103): Illuminate\Routing\AbstractRouteCollection->methodNotAllowed(Array, 'POST')
#1 /Applications/MAMP/htdocs/test-server/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php(40): Illuminate\Routing\AbstractRouteCollection->getRouteForMethods(Object(Illuminate\Http\Request), Array)
#2 /Applications/MAMP/htdocs/test-server/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php(162): Illuminate\Routing\AbstractRouteCollection->handleMatchedRoute(Object(Illuminate\Http\Request), NULL)
...