6

Is there any benefit to injecting the Request object into your controller method like this:

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:

class WidgetController
{
  public function create()
  {
    $name = request('name');
  }
}
wheelmaker
  • 2,975
  • 2
  • 21
  • 32
  • 2
    https://laravel.com/docs/5.6/facades#facades-vs-dependency-injection – Devon Bessemer Sep 23 '18 at 22:45
  • Thanks @devon, between that section you linked to and the Facades vs Helpers section just below, it's clearly laid out, guess I didn't search the docs well enough. – wheelmaker Sep 23 '18 at 23:14

4 Answers4

7

The request helper is just a shortcut to 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. 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.

There is really no difference, one is a shortcut of the other.

wheelmaker
  • 2,975
  • 2
  • 21
  • 32
Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31
5

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'). If app('request') is not initialized then your tests will generate an error.

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(). Then you can test the method and only the method without and dependencies to anything else.

Martijn Hiemstra
  • 927
  • 2
  • 14
  • 30
4

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.

Let's say your request must contain a parameter title and body. If the parameter is not there, it should never reach that endpoint. Using the helper(), there is not way to do it. 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');
  }
}
boka18
  • 307
  • 2
  • 11
  • You brought in a whole other component here, form requests. You can definitely still have validation rules without form request classes. – Devon Bessemer Sep 23 '18 at 23:02
  • 1
    You can actually validate like this request()->validate(['name' => ['required', 'string']]); I see the point for the validation technique you used though. – wheelmaker Sep 23 '18 at 23:03
  • You can also validate by calling the method `make` on the `Illuminate\Support\Facades\Validator` facade like this `$validator = Validator::make($request->all(), ['t' => 'required']);` – Yves Kipondo Sep 23 '18 at 23:07
  • @YvesKipondo just take a look at what you wrote, than take a look at my approach, and tell me which one is more readable. It's not only to make it work, you should also make it readable. And while you can validate with `make`, he was only asking the difference between `request()` vs dependency injection – boka18 Sep 24 '18 at 01:19
  • It isn't about readability when you specify the qualified class name any one who open you file will know that the method that you're calling is define in some `Request` class but when you call `request` He will go up and looking in your file He won't find the definition of that method directly unless He is knows the way laravel will handle that – Yves Kipondo Sep 24 '18 at 01:43
0

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.

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.

GTHell
  • 603
  • 1
  • 8
  • 20
  • I like this limitation as it deters me from passing the request object around. I think it's better to pass the service/business logic layer only the data that it needs - in your example you'd pass `$request->file('filename')` to `insertFile()`. – brad Mar 15 '22 at 16:22