Laravel 5 $request->input vs Input::get

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35316302/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:13:12  来源:igfitidea点击:

Laravel 5 $request->input vs Input::get

phplaravellaravel-5

提问by Jonh Doe

Just wondering what is the difference between:

只是想知道两者之间有什么区别:

$username = $request->input('username');

and

$username = Input::get('username');

回答by Jairo Correa

There is no difference, the facade Input calls the input method from request. But Input::getis deprecated, prefer the $request->inputinstead of Input::get

没有区别,外观 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';
    }   
}

回答by Yagnik Detroja

Both are the same but this one kind laravel inbuilt Functionality To make a proper use of laravel.

两者都是相同的,但是这种 laravel 内置功能可以正确使用 laravel。

You can use both way but following things are made only in INPUT. Just a look.

您可以同时使用这两种方式,但以下内容仅在 INPUT 中进行。只是看一眼。

  1. Input::has('name')

  2. Input::all()

  3. Input::only('username', 'password')

  4. Input::except('credit_card')

  5. Input::get('products.0.name')

  1. 输入::已('名称')

  2. 输入::全部()

  3. 输入::仅('用户名','密码')

  4. 输入::除了('credit_card')

  5. Input::get('products.0.name')

And also this on

还有这个

Input::get('username');

So that make things easy for as.

所以这让事情变得容易。

That other thing we have to do more code if we use this.

如果我们使用它,我们必须做更多的代码。

$request->input('username')

Hope You understand. Thanks.

希望你能理解。谢谢。