# 自定义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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liseen315.gitbook.io/laravel/bian-xie-apis/zi-ding-yi-404-xiang-ying.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
