• 13
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Laravel Request input() or get()

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?

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() 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.

  • 38
Reply Report
    • 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())
      • 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.
      • 1
    • 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.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions