【问题标题】:Variables in route group prefixes in Laravel 4Laravel 4 中路由组前缀中的变量
【发布时间】:2013-09-29 08:09:16
【问题描述】:

鉴于以下路线,它将响应

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');
        }]);
    });
});

【问题讨论】:

  • 我注意到您的某些路由参数以“$”为前缀,而有些则没有。
  • 哎呀,输入,但没有区别,因为参数名称仅在您使用路由绑定时才重要,我目前不这样做,所以重要的是顺序。

标签: php variables routing laravel laravel-4


【解决方案1】:

你能试试这个代码看看它是否是你想要的。这里的第二组路由只是 {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-07-29
  • 2013-01-05
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 2014-01-06
  • 2017-03-13
相关资源
最近更新 更多