简体   繁体   English

为什么在 Laravel 中对 Request 对象和 request() 助手使用依赖注入?

[英]Why use dependency injection for Request object vs request() helper in Laravel?

Is there any benefit to injecting the Request object into your controller method like this:像这样将 Request 对象注入控制器方法有什么好处:

use Request;

class WidgetController
{
  public function create(Request $request)
  {
    $name = $request->input('name');
  }
}

Versus eliminating the use statement and object injection and simply using the helper:与消除 use 语句和对象注入并简单地使用 helper 相比:

class WidgetController
{
  public function create()
  {
    $name = request('name');
  }
}

The request helper is just a shortcut to Request::input('name') . request助手只是Request::input('name')的快捷方式。 The code for the request helper is defined like this request helper请求助手的代码定义如下请求助手

app('request')->input($key, $default);

The app is the Container that manages the Dependency injection of Laravel.该应用程序是管理 Laravel 依赖注入的容器。 It will resolve the dependency that correspond to the name request which is an instance of Illuminate\\Http\\Request and call on it the method input passing the name of the key you want to retrieve.它将解析与作为Illuminate\\Http\\Request实例的名称request相对应的依赖项,并在其上调用方法input并传递您要检索的键的名称。

There is really no difference, one is a shortcut of the other.真的没有区别,一个是另一个的捷径。

The primary reason to use injection is because of testing.使用注入的主要原因是因为测试。 If you use request() then you need to initialize the Laravel app since request() calls app('request').如果你使用 request() 那么你需要初始化 Laravel 应用程序,因为 request() 调用 app('request')。 If app('request') is not initialized then your tests will generate an error.如果 app('request') 未初始化,则您的测试将生成错误。

When you use injection you pass on the Request object to the method.当您使用注入时,您将请求对象传递给方法。 This means that during testing you can create your own "dummy" request and pass that on to the method without having to initialize app().这意味着在测试期间,您可以创建自己的“虚拟”请求并将其传递给方法,而无需初始化 app()。 Then you can test the method and only the method without and dependencies to anything else.然后,您可以测试该方法,并且仅测试没有其他任何内容和依赖项的方法。

First of all, code-styling and readability.首先,代码样式和可读性。 The first one is way more readable.第一个更具可读性。 Second thing from top of my mind is that, if you use request() helper, you can not validate the request.我认为的第二件事是,如果您使用request()助手,则无法验证请求。

Let's say your request must contain a parameter title and body .假设您的请求必须包含参数titlebody If the parameter is not there, it should never reach that endpoint.如果参数不存在,则它永远不会到达该端点。 Using the helper() , there is not way to do it.使用helper() ,没有办法做到这一点。 While, using the first method, there is really convenient way of doing that.同时,使用第一种方法,确实有很方便的方法。

class StoreRequest extends FormRequest
{
    public function rules()
    {
        return [
            'title' => 'required',
            'body' => 'sometimes'
        ];
    }
}

And than just:而不仅仅是:

use StoreRequest;

class WidgetController
{
  public function create(StoreRequest $request)
  {
    $name = $request->input('name');
  }
}

Might be late but useful information about using request() helper is that you don't need to pass request object to your business logic classes.可能会迟到,但有关使用 request() 助手的有用信息是您不需要将请求对象传递给您的业务逻辑类。

For instance:例如:

User.php
-----------------------------------------------
...
protected $helper;

public function __construct(FileHelper $helper) {
    $this->helper = $helper

public function uploadFile() {
    $file = $this->helper->insertFile();
}
...
-----------------------------------------------


FileHelper.php
-----------------------------------------------
...

public function insertFile() {
    $file = request()->file('filename');
    // ur code 

}
...
-----------------------------------------------

See that you don't need to pass $request to your insertFile since the request helper globally injected into your app.请注意,您不需要将 $request 传递给您的 insertFile,因为请求助手已全局注入您的应用程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM