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

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

use Illuminate\Auth\AuthenticationException; 
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
Summer
最佳答案

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

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

    return redirect()->guest('login'); //<----- 修改这里
}
6年前 评论
讨论数量: 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'); //<----- 修改这里
}
6年前 评论

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

6年前 评论

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

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

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

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

6年前 评论
Summer

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

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

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

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

6年前 评论

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

file

5年前 评论

@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'));
    }
5年前 评论

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

5年前 评论

@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 方法
5年前 评论

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

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

5年前 评论

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

5年前 评论

在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); }

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

修改底下的那个'login'

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!