2016-03-05 60 views
1

我遇到的問題是,如果在我的get請求中傳遞可選參數的值,那麼得到的結果與傳遞值不變時的結果相同。無法從Laravel中的獲取請求中檢索正確的參數值

路線

Route::get('/play/game/{new?}', [ 
     'uses' => '[email protected]', 
     'as' => 'game' 
    ]); 

控制器

public function getGame($new = null) { 
    $quotes = Quote::orderBy('created_at', 'dec')->paginate(50);  

    if(!is_null($new)) { 
     if ($new == 'yes') 
      $newgame = true; 
    } else { 
     $newgame = false; 
    } 

    return view('game', ['quotes' => $quotes, 'newgame' => $newgame]);  
} 

查看

@if($newgame ==true) 
{{Session::flush()}} 
@endif 

@if($newgame == false) 
<h1>Not a new game</h1> 
@endif 

第二,如果在聲明視圖只是一個測試,而且這是總是執行的語句。 僅供參考,我正在使用Laravel 5.2

回答

1

如下您可以註冊路線:

Route::get('play/game/', [ 
     'uses' => '[email protected]', 
     'as' => 'game' 
    ]); 

,然後當你調用url,通過這樣的變量:

play/game/?new=yes

然後在你的控制器,你可以檢查變量是否設置:

if($request->input('new')=='yes' 

檢查,如果參數存在:

$request->has('new') 
+0

這個工程!謝謝 (: – Frosty619

0

試試這個 我認爲沒有必要把'?'在參數符號

Route::get('play/game/{new}', [ 
     'uses' => '[email protected]', 
     'as' => 'game' 
    ]); 
+0

實際上,?符號代表一個可選參數,所以如果沒有值傳遞,Laravel將不會發瘋並拋出錯誤。你可以在這裏閱讀:https://laravel.com/docs/5.1/routing – Frosty619

+0

哦對不起。即使我不這樣做。 – utsav