ModelNotFoundException 异常回调没有执行?
首先在异常处理类编写回调函数
app/Exceptions/Handler.php
use Illuminate\Database\Eloquent\ModelNotFoundException;
...
public function register()
{
/*$this->reportable(function (Throwable $e) {
//
});*/
$this->renderable(function (ModelNotFoundException $e, $request) {
return response()->json([
'error' => 'Resource not found.'
],404);
});
}
路由和控制器
Route::get('articles/{article}', [ArticleController::class, 'show']);
...
public function show(Article $article){
return $article;
}
访问一个不存在的article id
// 只有50条记录
http://localhost/api/articles/100
// 此时异常却没有被被捕获,按道理不是返回json么
推荐文章: