Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisgo's avatar

request() vs $request

Hi,

In the 5.5 documentation, the way to get the request is

public function index(Request $request) {
  $value = $request->input('test');
  $input = $request->all();  
}

then there is a helper so this works too

public function index() {
  $value = request('test');
  $input = request()->all();
}

is there a different between the 2? Using the helper request() seems a little bit cleaner but not sure if that going to yield the same results if we make that our coding standard

0 likes
3 replies
Talinon's avatar
Talinon
Best Answer
Level 51

It is a choice that comes down purely to personal preference.

request() is just a helper function that returns the same Request object.

You can see this for yourself by simply dumping each:

dump($request);
dd(request());

Also, request() can be called from anywhere within your app without the need to reference the full namespace (Illuminate\Http\Request)

2 likes
gilesrussi's avatar

I prefer using dependency injection in most cases. I had a problem using an .env.testing with the request object because the request that I was getting was the request from the application, not the one from testing. And I had a hard time finding it out what was the cause. Turns out I was using "request()" when I should have used the $request from the correct environment.

But again, edge cases. You could do just fine with "request()" and never have problems with it.

3 likes

Please or to participate in this conversation.