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

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

问题描述

基于 Input Facade APIRequest Facade API Input::get() 方法似乎是唯一的区别.我在这里遗漏了什么吗?

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.

推荐答案

是的,两个 Facades 非常相似.这样做的原因是底层类是相同的(IlluminateHttpRequest).您可以通过查看 Facade 类及其访问器来看到这一点:

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

IlluminateSupportFacadesInput

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

IlluminateSupportFacadesRequest

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


正如您所意识到的,一个区别是 Input::get() 方法.这只是翻译"直接在 Facade 中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.Input 在 5.0 的文档中没有提及.它并未(正式)弃用,但鼓励使用 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 的一点是,外观实际上具有底层类的名称.这样就很清楚你在处理什么.然而,这也可能是错误的根源.如果您使用类似 Request::input('name') 的内容,请确保使用 use Request;use IlluminateSupportFacadesRequest 导入 Facadenot 使用 IlluminateHttpRequest.依赖注入则相反.

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 IlluminateSupportFacadesRequest and not use IlluminateHttpRequest. The opposite applies for dependency injection.

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

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