In Laravel < 5.5 I could change this file app/Exceptions/Handler to change the unauthenticated user redirect url:

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

    return redirect()->guest(route('login'));
}

But in Laravel 5.5 this has been moved to this location vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php so how can I change it now? I don't want to change stuff in the vendor directory encase it gets overridden by composer updates.

protected function unauthenticated($request, AuthenticationException $exception)
{
    return $request->expectsJson()
                ? response()->json(['message' => 'Unauthenticated.'], 401)
                : redirect()->guest(route('login'));
}

推荐答案

But in Laravel 5.5 this has been moved to this location vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php so how can I change it now? I don't want to change stuff in the vendor directory encase it gets overridden by composer updates.

只是默认情况下该函数不再存在.

您只需像在5.4中所做的那样覆盖它.只要确保包括

use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;

在处理程序文件中.

例如,我的app/Exceptions/Handler.php看起来有点像这样:

<?php
    namespace App\Exceptions;
    use Exception;
    use Request;
    use Illuminate\Auth\AuthenticationException;
    use Response;
    use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
    class Handler extends ExceptionHandler
    {
        (...) // The dfault file content
        /**
         * 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' => 'Unauthenticated.'], 401)
                    : redirect()->guest(route('authentication.index'));
    }
}

Laravel相关问答推荐

Laravel Livewire与自定义JS有关的多姆问题

在 Laravel 中排序的集合在 Vue 中突然不再排序

无法找到用户sail:passwd 文件中没有匹配的条目

处理程序类中的错误 - Laravel

Laravel & Docker:无法打开流或文件/var/www/html/storage/logs/laravel.log:无法打开流:权限被拒绝

如何访问 Validator::extend 中的其他输入属性?

在 laravel 的集合中找到?例子

如何在 laravel 中安装 PHPExcel 库?

Laravel Request::input 调用未定义的方法

合并和排序两个 Eloquent 集合?

Laravel Eloquent Pluck 不丢失密钥

Laravel 5 文件下载:stream() 或 download()

Laravel 模型:模型属性在哪里?

Laravel 中的单会话登录

如何清理 Laravel Bootstrap 缓存配置文件?

Laravel Queue,Beanstalkd vs Database,有什么区别?

Grammar::parameterize() 必须是数组类型

Laravel 部署……有标准的方式吗?

如何使用队列设置高、低和中优先级Electron邮件?

Laravel 将附加参数传递给函数