php Laravel 请求 input() 或 get()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30186169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Request input() or get()
提问by Yada
With Laravel 5 it seems like method injection for the Request object is preferred over using the Request facade.
在 Laravel 5 中,请求对象的方法注入似乎比使用请求门面更受欢迎。
<?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
使用Illuminate\Http\Request
比使用更好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.
我不知道如何$请求- > get()方法被解决,因为没有函数名get()
在Illuminate\Http\Request
。input() 和 get() 做同样的事情。
Is method injection better than using Facades?
方法注入比使用 Facades 更好吗?
回答by Maxim Lanin
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.
此外,对象注入总是比 Facades 好,因为这样的方法和对象更容易测试。
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.
get()
并且input()
是不同类的方法。第一个是 Symfony HttpFoundation Requestinput()
的方法,是 Laravel Request 类的一个方法,它扩展了 Symfony Request 类。