56

With Laravel 5 it seems like method injection for the Request object is preferred over using the Request facade.

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index(Request $request)
    {
        $email = $request->input('email');

        // OR

        $email = $request->get('email');
    }
}

A few questions I have:

Is using Illuminate\Http\Request better than using Illuminate\Support\Facades\Request

I have no idea how $request->get() is resolving as there is no function name get() in Illuminate\Http\Request. input() and get() does the same thing.

Is method injection better than using Facades?

Yada
  • 30,349
  • 24
  • 103
  • 144

1 Answers1

59

Injection vs Facade

In controller method Request injection functionality is always preferable, because in some methods it could help you to use Form Requests (they are extending default Request class) validation, that will validate your request automatically just before entering to the actual controller method. This is an awesome feature that helps to create slim and clean controller's code.

Using default Request injection makes your controller's methods similar and easier to maintain.

Also object injection is always better than Facades, because such methods & objects are easier to test.

get vs input

get(...) andinput(...) are methods of different classes:

  • First one is method of Symfony HttpFoundation Request,
  • input() is a method of the Laravel Request class that is extending Symfony Request class, and it supports dot notation to access nested data (like $name = $request->input('products.0.name')).
Top-Master
  • 7,611
  • 5
  • 39
  • 71
Maxim Lanin
  • 4,351
  • 24
  • 32
  • 3
    Nice answer. I personally prefer `input()` and I believe it is officially preferred to `get()`. I especially like it because it avoids confusion with `GET` and `POST` input data. (this was a big problem in earlier versions where you had `Input::get()`) – lukasgeiter May 12 '15 at 09:52
  • 2
    `::get()` certainly is confusing because it checks both GET and POST vars. As max has pointed, using the `Request` class allows you to very quickly swap it out into your own Request object, such as a validation request. – dotty May 12 '15 at 12:51
  • What do you mean by : `get()` check both GET and POST vars.? doesn't `input()` do that? – alex Dec 31 '15 at 15:53
  • @alex get() is an alias of the input(), as far as I remember. Or vice versa. – Maxim Lanin Jan 02 '16 at 14:22
  • @lukasgeiter Is there another reason why input() is preferred other than the confusion with get() and GET/POST? – Bryan Miller Feb 23 '16 at 01:02
  • @user3089840 Besides the ones I previously mentioned none that I can think of. – lukasgeiter Feb 23 '16 at 05:31
  • I noticed the code appears quite different between the two and wonder why it wasn't just renamed to begin with, or an alias created. – Bryan Miller Feb 23 '16 at 15:14
  • 18
    With `input` you can use the dot notation to access nested data `$name = $request->input('products.0.name');` Doesn't work for `get` i believe. – Henri Toivar Jul 08 '16 at 07:58
  • @HenriToivar You are absolutely right, `get` won't let you grab nested data using the dot notation; `input` will. – David Routen Sep 23 '16 at 18:26
  • @HenriToivar Also, if you have a Laravel `Collection` as a parameter value (for example, you have transformed request parameter with a middleware) with one instance, than `get()` will be equal to `->first()` and `input()` will return the whole `Collection`. – D.R. Jan 10 '18 at 11:02