Php 如何向Laravel 5.5中的处理程序添加未经验证的检查?

Php 如何向Laravel 5.5中的处理程序添加未经验证的检查?,php,laravel-5,Php,Laravel 5,我修改了app/Exceptions/Handler.php,使其具有未经验证的函数。因为它纯粹是一个API,所以我只返回json protected function unauthenticated($request, AuthenticationException $exception) { return response()->json(['error' => 'Unauthenticated'], 401); } 添加了php artisan tinker后,我发现

我修改了app/Exceptions/Handler.php,使其具有未经验证的函数。因为它纯粹是一个API,所以我只返回json

protected function unauthenticated($request, AuthenticationException $exception)
{
    return response()->json(['error' => 'Unauthenticated'], 401);
}
添加了
php artisan tinker
后,我发现了以下错误

[ErrorException]
  Declaration of App\Exceptions\Handler::unauthenticated($request, App\Exceptions\AuthenticationException $exception) should be compatible with Illuminate\Foundation\Exceptions\Handler::unauthenticated($request, Illuminate\Auth\AuthenticationException $exception)

但我不确定需要改变什么才能使它兼容。I idi添加一个use语句
use-illumb\Auth\authenticationException到处理程序的顶部。

您也应该使用Exception和request

请确保包括以下内容:

namespace App\Exceptions;

use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest('/');
}
你的方法应该是这样的:

namespace App\Exceptions;

use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest('/');
}
您的代码只响应为json