2

鉴于以下路线,它将响应

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){

但这失败了“缺少 {closure}() 的参数 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');
        }]);
    });
});
4

1 回答 1

6

你可以试试这个代码看看它是否是你想要的。这里是第二组路线,它只是{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']) }}
于 2013-09-24T16:12:41.750 回答