> For the complete documentation index, see [llms.txt](https://liseen315.gitbook.io/laravel/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liseen315.gitbook.io/laravel/bian-xie-apis/zi-ding-yi-404-xiang-ying.md).

# 自定义404响应

Laravel为普通HTML视图提供可自定义的错误消息页面，但您也可以为具有JSON内容类型的调用自定义默认的404回退响应。 为此，请向您的API添加Route::fallback()调用，如例13-42所示

{% code title="Example 13-42. Defining a fallback route" %}

```php
// routes/api.php
Route::fallback(function () {
    return response()->json(['message' => 'Route Not Found'], 404);
})->name('api.fallback.404');
```

{% endcode %}
