2013-09-24 66 views
1

鑑於以下航線將到變量在Laravel路由組前綴4

http://example.com/game/stats/123
http://example.com/game/stats/game/123
http://example.com/game/stats/reviewer/123

我想知道的是反應,我怎樣才能使它迴應

http://example.com/game/123/stats
http://example.com/game/123/stats/game
http://example.com/game/123/stats/reviewer

我試圖做

Route::group(['prefix' => 'game/{game}'], function($game){ 

但失敗與 「缺少參數1 {關閉}()」

注意,還有其他四組分開從統計數據來看,但爲了簡潔起見,我省略了這些數據。

Route::group(['prefix' => 'game'], function(){ 
    Route::group(['prefix' => 'stats'], function(){ 
     Route::get('/{game}', ['as' => 'game.stats', function ($game) { 
      return View::make('competitions.game.allstats'); 
     }]); 
     Route::get('game/{game}', ['as' => 'game.stats.game', function ($game) { 
      return View::make('competitions.game.gamestats'); 
     }]); 
     Route::get('reviewer/{game}', ['as' => 'game.stats.reviewer', function ($game) { 
      return View::make('competitions.game.reviewstats'); 
     }]); 
    }); 
}); 
+0

我注意到你的一些路由參數的前綴是'$',有些不是。 – JofryHS

+0

哎呀,類型,但它沒有什麼區別,因爲參數名稱只在您使用路由綁定時很重要,我現在不是這樣,所以它只是重要的順序。 – Hailwood

回答

5

你可以試試這段代碼,看看它是你想要的。這裏第二組路由它只是{gameId},然後你有stats組包裝所有其他路由。

Route::group(['prefix' => 'game'], function(){ 
     Route::group(['prefix' => '{gameId}'], function(){ 
     Route::group(['prefix' => 'stats'], function(){ 
      Route::get('/', ['as' => 'game.stats', function ($game) { 
       return View::make('competitions.game.allstats'); 
      }]); 
      Route::get('game', ['as' => 'game.stats.game', function ($game) { 
      return View::make('competitions.game.gamestats'); 
      }]); 
      Route::get('reviewer', ['as' => 'game.stats.reviewer', function ($game) { 
      return View::make('competitions.game.reviewstats'); 
      }]); 
     }); 
     }); 
    }); 

然後在你的意見,你可以通過路線名字稱呼他們,並傳遞gameId的路線;

{{ link_to_route('game.stats','All Stats',123) }} // game/123/stats/ 
{{ link_to_route('game.stats.game','Game Stats',123) }} // game/123/stats/game 
{{ link_to_route('game.stats.reviewer','Review Stats',123) }} // game/123/stats/reviewer 

希望這有助於解決您的問題。

編輯

我只是檢查它應與Route::group(['prefix' => 'game/{game}'也作爲你嘗試,但只要確保創建類似上述的路由時傳遞game說法。如果你有更多的變量可以傳遞,你可以傳遞一個數組給函數。

{{ link_to_route('game.stats','All Stats',['game' => '123','someOtherVar' => '456']) }} 
+0

啊!這是我的問題,因爲當你將變量放在路由定義中時,你需要將變量添加到路由閉包簽名中,因此您需要在路由組閉包中執行相同的操作。 – Hailwood