Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 695
Yada Avatar asked May 12 '15 08:05

Yada


People also ask

What is request -> input () in Laravel?

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

How does request work in Laravel?

Once the application has been bootstrapped and all service providers have been registered, the Request will be handed off to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware.


1 Answers

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')).
like image 84
Maxim Lanin Avatar answered Sep 22 '22 09:09

Maxim Lanin