2016-02-10 60 views
1

只是想知道是什麼樣的區別:

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

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

沒有什麼他們是完全一樣的,除了一個事實,即'輸入:: GET'被棄用(它沒有任何地方提及,但可以假設請求是首選閱讀文檔時的方式),因爲在laravel 5.0和更高版本的文檔中沒有提及它。 – Daan

+1

https://laravel.com/docs/4.2/requests檢查 –

+0

laravel.com/docs/4.2/requests只提及Input :: get,https://laravel.com/docs/5.2/requests只提及$請求 - >輸入 –

回答

6

沒有區別,facade輸入從請求調用輸入方法。但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'; 
    } 
} 
+0

這個文件在哪裏? –

+1

我發佈的代碼是'vendor/laravel/framework/src/Illuminate/Support/Facades/Input.php'中的Facade Input –

1

兩者都是相同的,但這樣一種laravel內置功能要正確使用laravel。

您可以同時使用這兩種方式,但只能在INPUT中進行以下操作。只是看看。

  1. 輸入::有( '名稱')

  2. 輸入::所有()

  3. 輸入::只( '用戶名', '密碼')

  4. Input :: except('credit_card')

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

而且這也對

Input::get('username'); 

所以這讓事情容易的。

如果我們使用它,我們還需要做更多的代碼。

$request->input('username') 

希望你明白。 謝謝。

+0

這不是真的,Input只是請求的Facade,Request也是請求的Facade,這意味着Input使用請求的方法,如果你想使用Facade來實用,你應該使用Facade Request而不是Input,例如'Request :: all(); Request :: has('name');' –