vlio20
vlio20

Reputation: 9295

Laravel routes error

Here is two routes that I have:

Route::resource('tournament/{tourId}/phase/{phaseId}/bets', 'PhaseController@getPhaseBets');
Route::resource('tournament/{tourId}/phase/{phaseId}', 'PhaseController@getPhaseInfo');

But I am getting this exception when trying to run any GET api:

Route pattern "/api/tournament/{tourId}/phase/{phaseId}/{{phaseId}}" cannot reference variable name "phaseId" more than once.

From did it get {{phaseId}} ? why I am getting this error?!

Upvotes: 0

Views: 1914

Answers (1)

Unnawut
Unnawut

Reputation: 7578

TL;DR:

Since you are defining routes directly pointing to your controller actions, you should be using:

Route::get('tournament/{tourId}/phase/{phaseId}/bets', 'PhaseController@getPhaseBets');
Route::get('tournament/{tourId}/phase/{phaseId}', 'PhaseController@getPhaseInfo');

Longer version:

To use Route::resource() you should be following the docs in Resource Controllers. E.g:

Route::resource('phase', 'PhaseController');

Which will generate route urls like:

/tournament/{tourId}/phase/{phaseId}/bets
/tournament/{tourId}/phase/{phaseId}/bets/{resource_id}
/tournament/{tourId}/phase/{phaseId}/bets/{resource_id}/edit
/tournament/{tourId}/phase/{phaseId}/bets/create

You would see that now instead of having one parameter for the resource id, the routes now have up to 3 parameters. That confuses Laravel up on which one it should use as the resource id.

Since you are defining routes directly pointing to your controller actions, you should be using:

Route::get('tournament/{tourId}/phase/{phaseId}/bets', 'PhaseController@getPhaseBets');
Route::get('tournament/{tourId}/phase/{phaseId}', 'PhaseController@getPhaseInfo');

If you'd still like to use resource controllers you'll need to change your code to something like this.

Route::resource('phase', 'PhaseController');
Route::resource('bet', 'BetController');

and in your controller files:

PhaseController.php:

class PhaseController extends BaseController
{
    public function show($id)
    {
        // Your original PhaseController@getPhaseInfo() here
    }
}

BetController.php:

class BetController extends BaseController
{
    public function show($id)
    {
        // Your original PhaseController@getPhaseBets() here
    }
}

And reference your urls like this:

/phase/your_phase_id
/bet/your_bet_id

Obviously this might not totally suit your requirements. If you could explain a little bit of the relationships between tournaments, phases, bets, and what you're trying to achieve, maybe we can suggest a better way.

Upvotes: 5

Related Questions