# 使用表单请求

既然我们已经创建了表单请求,那么如何使用它呢？在Laravel中可以将创建的请求作为参数传递给闭包或者是控制器方法。

让我们看一下示例7-18

{% code title="Example 7-18. Using a form request" %}

```php
Route::post('comments', function (App\Http\Requests\CreateCommentRequest $request) { 
    // Store comment
});
```

{% endcode %}

它验证用户输入和请求认证，如果输入无效，则像Request的validate()一样，将用户重定向到上一页，保留其输入并传递错误消息，如果用户未经授权，Laravel将返回403禁止错误，不执行路由代码。


---

# 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/shou-ji-he-chu-li-yong-hu-shu-ju/biao-dan-qing-qiu/chuang-jian-yi-ge-biao-dan-qing-qiu-1.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.
