打開appExceptionsHandler.php文件,重寫unauthenticated方法:php
/** * 重寫實現未認證用戶跳轉至相應登錄頁 * @param \Illuminate\Http\Request $request * @param AuthenticationException $exception * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse */ protected function unauthenticated($request, AuthenticationException $exception) { //return $request->expectsJson() // ? response()->json(['message' => $exception->getMessage()], 401) // : redirect()->guest(route('login')); if($request->expectsJson()){ return response()->json(['message' => $exception->getMessage()], 401); }else{ return in_array('admin', $exception->guards()) ? return redirect()->guest('/admin/login') : redirect()->guest('login'); } }