Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

php - Laravel Input Facade vs Request Facade

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...