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']) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥20 python作业求过程
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录
  • ¥15 关于#Django#的问题:我的静态文件呢?