Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Input Facade vs Request Facade

Tags:

Based on Input Facade API and Request Facade API the Input::get() method seems to be the only difference. Am I missing something here?

I know Validation can be applied to Requests, but I am not sure if the same is true for the Input facade.

like image 645
CrackingTheCode Avatar asked Apr 30 '15 06:04

CrackingTheCode


People also ask

What is request facade?

The Request facade will grant you access to the current request that is bound in the container. For example: $name = Request::input('name'); Remember, if you are in a namespace, you will have to import the Request facade using a use Request; statement at the top of your class file.

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

Why facade is important in Laravel?

Laravel ships with many facades which provide access to almost all of Laravel's features. Laravel facades serve as "static proxies" to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.


1 Answers

Yes both Facades are very similar. The reason for this is that the underlying class is the same (Illuminate\Http\Request). You can see this by looking at both Facade classes and their accessors:

Illuminate\Support\Facades\Input

protected static function getFacadeAccessor() {     return 'request'; } 

Illuminate\Support\Facades\Request

protected static function getFacadeAccessor() {     return 'request'; } 

As you realized, one difference is the Input::get() method. This is just "translated" to Request::input() directly in the Facade:

public static function get($key = null, $default = null) {     return static::$app['request']->input($key, $default); } 

Conclusion

They are essentially the same. That means, there's no need to change your existing code. However if you wanted to it wouldn't make any difference.

When writing new code you should use Request facade, and maybe use Request::input(...) instead of Request::get(...) (because the former supports dot notation to access nested data, like $name = $request->input('products.0.name');, but get is little faster to be fair).

Input is mentioned nowhere in the documentation for 5.0. It's not (officially) deprecated but the use of Request is encouraged.

What I also really like about Request is that the Facade actually has the name of the underlying class. This way it's clear what you're dealing with. However this can also be the root of errors. If you use something like Request::input('name') make sure to import the Facade with use Request; or use Illuminate\Support\Facades\Request and not use Illuminate\Http\Request. The opposite applies for dependency injection.

like image 184
lukasgeiter Avatar answered Sep 19 '22 20:09

lukasgeiter