教程中 “默认将会被重定向到 /login 登录页面”,请问如何修改?

用了答案中的方法之后,在顶部还要加上,否则会报错

use Illuminate\Auth\AuthenticationException; 
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
Summer
最佳答案

app/Exceptions/Handler.php 里新增以下方法:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest('login'); //<----- 修改这里
}
7年前 评论
讨论数量: 13
Summer

app/Exceptions/Handler.php 里新增以下方法:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest('login'); //<----- 修改这里
}
7年前 评论

没看懂 你可以说的详细一些

7年前 评论

看你的意思,应该是被中间件重定向了

7年前 评论
//Controller/Auth/LoginController
class ForgotPasswordController extends Controller{

    protected $redirectTo = '一个自定义的登陆界面'
    ...
}

建议查阅文档 https://learnku.com/docs/laravel/5.3/authe...

7年前 评论
Summer

app/Exceptions/Handler.php 里新增以下方法:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest('login'); //<----- 修改这里
}
7年前 评论

大佬上面的那个方法是通过覆盖 Illuminate\Foundation\Exceptions\Handler 的 unauthenticated 方法实现的,可是覆盖了报出
should be compatible with 错误,上网查的方法好像也没用

7年前 评论

@g1f9 我也是,用作者的方法覆盖了之后,目前报错

file

7年前 评论

@laravel 初探者
默认转向方法在 Illuminate\Foundation\Exceptions\Handler 中,我更改了其中的 redirect () 便好了 /**

  • Convert an authentication exception into a response.
  • @param \Illuminate\Http\Request $request
  • @param \Illuminate\Auth\AuthenticationException $exception
  • @return \Illuminate\Http\Response */ protected function unauthenticated($request, AuthenticationException $exception) { return $request->expectsJson() ? response()->json(['message' => $exception->getMessage()], 401) redirect()->guest(route('sessions.create'));
    }
7年前 评论

@熊能 我直接在框架中的默认重定向改了,没有重写这个方法

7年前 评论

@laravel 初探者

  • 直接在原 C:\Users\zhu\Code\sample2\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php 文件中直接更改最简单粗暴有效;
  • 规范做法还是在 C:\Users\zhu\Code\sample2\app\Exceptions\Handler.php 中自己重写 unauthenticated 方法
6年前 评论

怎么重写 unauthenticated 方法后而且加上 use Illuminate\Auth\AuthenticationException; 还是不行啊

!问题解决了__construct 下划线是两条我写成一条了……

6年前 评论

@Summer 我看了源码,但是没找到是怎么实现跳到 login 页面的,不知道如果有时间能不能帮忙解答一下.
谢谢.

6年前 评论

在 App\Exceptions\Handler 下,修改 render 方法 public function render ($request, Exception $exception) { if ($exception instanceof AuthenticationException) { return response ()->json ([ 'code' => xxx, 'msg' => xxxx, 'data' => [] ], 401); } return parent::render ($request, $exception); }

5年前 评论
app/Http/Middleware/Authenticate.php

修改底下的那个 'login'

4年前 评论