Hailwood
Hailwood

Reputation: 92691

Variables in route group prefixes in 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');
        }]);
    });
});

Upvotes: 2

Views: 4919

Answers (1)

Altrim
Altrim

Reputation: 6756

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

Upvotes: 6

Related Questions