Laravel输入外观vs请求外观 [英] Laravel Input Facade vs Request Facade

查看:99
本文介绍了Laravel输入外观vs请求外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于输入外观API

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.

推荐答案

是的,两个立面都非常相似.这样做的原因是基础类是相同的(Illuminate\Http\Request).通过查看Facade类及其访问器,您可以看到这一点:

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';
}


您已经意识到,Input::get()方法是其中之一.这只是直接在立面中翻译"为Request::input():


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.

编写新代码时,应使用Request. 5.0文档中未提及Input. (正式)不建议弃用,但鼓励使用Request.

When writing new code you should use Request. Input is mentioned nowhere in the documentation for 5.0. It's not (officially) deprecated but the use of Request is encouraged.

我对Request的真正想法是Facade实际上具有基础类的名称.通过这种方式,您可以清楚地了解正在处理的内容.但是,这也可能是错误的根源.如果您使用Request::input('name')之类的东西,请确保使用use Request;use Illuminate\Support\Facades\Request use Illuminate\Http\Request导入外观.对于依赖项注入则相反.

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.

这篇关于Laravel输入外观vs请求外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆