7

在其他 MVC 框架中,访问当前请求对象就像$this->request. 但是在 Laravel 中,我通常看到它Request $request通常被注入到每个动作(public function edit($id, Request $request))中。这似乎是一个样板。有没有更好的方法来访问请求?(我现在可以使用继承来使用$this->request,我正在寻找 Laravel 的方式来做到这一点。)

更新:

我发现使用app('request')我可以访问当前请求。但是,我不确定它的潜在利弊。

4

1 回答 1

6

在 Laravel 5 中,你可以使用request()helper:

// to get the current request object
$request = request();

// or to just get a value from the request
$value = request("field", "default");

https://laravel.com/docs/5.6/helpers#method-request

于 2018-07-06T20:47:40.347 回答