[Laravel] Laravel 5.5 教你修改Auth認證失敗跳轉位置

Laravel5.5
從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'));
    }

留言

這個網誌中的熱門文章

[PHP] 正規表達式 過濾特殊符號 / 過濾非字母數字的字元 / 過濾字母數字