想知道以下两者之间有何区别:
$username = $request->input('username');
并且
$username = Input::get('username');
没有区别,外观模式Input从请求中调用输入方法。但Input::get
已过时,请使用$request->input
代替Input::get
<?php
namespace Illuminate\Support\Facades;
/**
* @see \Illuminate\Http\Request
*/
class Input extends Facade
{
/**
* Get an item from the input data.
*
* This method is used for all request verbs (GET, POST, PUT, and DELETE)
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($key = null, $default = null)
{
return static::$app['request']->input($key, $default);
}
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'request';
}
}
vendor/laravel/framework/src/Illuminate/Support/Facades/Input.php
。 - Jairo Correa两者都是相同的,但这个内置了laravel功能,以便更好地使用laravel。
你可以两种方式都使用,但以下事项仅在INPUT中提供。只需一看。
Input::has('name')
Input::all()
Input::only('username', 'password')
Input::except('credit_card')
Input::get('products.0.name')
还有这个
Input::get('username');
这样可以让事情更加简单。
如果我们使用这种方法,就需要编写更多的代码。
$request->input('username')
Request::all(); Request::has('name');
- Jairo Correa
Input::get
已经被弃用(在文档中没有明确说明,但可以假设在阅读文档时使用 Request 是首选方式),因为在 Laravel 5.0 及更高版本的文档中没有提到它。 - DaanInput
外观已被替换为Request
(https://laravel.com/docs/6.x/upgrade#the-input-facade)。 - Mateusz