[Laravel] Laravel 5.5 教你修改Auth認證失敗跳轉位置
從5.3開始,未認證跳轉的判斷搬到了app\Exceptions\Handler.php中處理。今天使用5.5,發現unauthenticated方法不見了,自己加還會有衝突,不過沒關係,5.5是搬家到了vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php中,修改方式如下: 加入下段程式碼即可 /** * Convert an authentication exception into an unauthenticated response. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Auth\AuthenticationException $exception * @return \Illuminate\Http\Response */ protected function unauthenticated($request, AuthenticationException $exception) { if ($request->expectsJson()) { return response()->json(['error' => 'Unauthenticated.'], 401); } return redirect()->guest(route('admin.login')); }