douwen1915 2013-09-24 08:00
浏览 74
已采纳

Laravel 4中路由组前缀的变量

Given the below routes it will respond to

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

What I want to know is, how can I make it respond to

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

I tried doing

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

But that fails with "Missing argument 1 for {closure}()"

Note that there are four other groups apart from stats but I have omitted them for this example for brevity.

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');
        }]);
    });
});
  • 写回答

1条回答 默认 最新

  • dsu89430 2013-09-24 16:12
    关注

    Can you try this code see if it's what you want. Here the second group route it's just the {gameId}and then you have the stats group which wraps all the other routes.

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

    And then in your views you can call them by the route name and pass the gameId to the route;

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

    Hope this helps and solves your problem.

    EDIT

    I just checked It should work also with Route::group(['prefix' => 'game/{game}' as you have tried but just make sure to pass the game argument when creating the route like stated above. If you have more variables to pass you can pass an array to the function.

    {{ link_to_route('game.stats','All Stats',['game' => '123','someOtherVar' => '456']) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计