Laravel5.5重写实现未通过认证(多用户)跳转相应登陆页面

打开appExceptionsHandler.php文件,重写unauthenticated方法:

    /**
     * 重写实现未认证用户跳转至相应登陆页
     * @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');
        }
    }
    原文作者:hinet
    原文地址: https://segmentfault.com/a/1190000012508645
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞