问答 / 0 / 5 / 创建于 4年前
jwt-auth怎么自定义错误返回啊?我不是不看文档,看了半天愣是没找到
我是修改了 app/Middleware/Authenticate.php 文件:
<?php namespace App\Http\Middleware; use App\Exceptions\ApiException; use Illuminate\Auth\Middleware\Authenticate as Middleware; class Authenticate extends Middleware { protected function redirectTo($request) { if ($request->expectsJson()) { return error('token 已失效,请重新登录', 401); } else { return route('login'); } } }
error 方法:
error
app/helpers.php
if (! function_exists('error')) { function error($message, $code = -1) { throw new \App\Exceptions\ApiException($message, $code); } }
ApiException 是使用 artisan 创建的异常类,没有修改代码
ApiException
artisan
在 app/Exceptions/Handler.php 中的 render 方法自定义响应异常:
render
public function render($request, Throwable $exception) { return response()->json([ 'code' => $code, 'message' => $message, ]); }
我要举报该,理由是:
我是修改了 app/Middleware/Authenticate.php 文件:
error
方法:app/helpers.php
ApiException
是使用artisan
创建的异常类,没有修改代码在 app/Exceptions/Handler.php 中的
render
方法自定义响应异常: