Laravel 4中路由组前缀的变量

Hai*_*ood 1 php variables routing laravel 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){
Run Code Online (Sandbox Code Playgroud)

但是失败的是"缺少{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');
        }]);
    });
});
Run Code Online (Sandbox Code Playgroud)

Alt*_*rim 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');
          }]);
        });
      });
    });
Run Code Online (Sandbox Code Playgroud)

然后在您的视图中,您可以通过路径名称呼叫它们并将其传递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
Run Code Online (Sandbox Code Playgroud)

希望这有助于解决您的问题.

编辑

我刚刚检查它应该也可以正常工作Route::group(['prefix' => 'game/{game}'但只是确保game在创建如上所述的路径时传递参数.如果要传递更多变量,可以将数组传递给函数.

{{ link_to_route('game.stats','All Stats',['game' => '123','someOtherVar' => '456']) }}
Run Code Online (Sandbox Code Playgroud)