zarcel
zarcel

Reputation: 1241

How to use id and /route in Laravel?

I have 2 routes:

/route/{id}
/route/add

First one shows something based on id provided and second one provies form for adding news item. My problem is that when I hit /route/add it takes the add word and treats it as id for the first route. Is there any solution for this in laravel or do I have to use different route names?

Upvotes: 0

Views: 1900

Answers (2)

Jonathon
Jonathon

Reputation: 16283

Routes work on a first-come first-served basis. The earlier routes are looked at first. Since you have a wildcard {id} on your first of those two routes, Laravel is treating add as {id} and will be passing off to that controller/closure.

You should switch the two routes around like this:

Route::get('route/add', 'Controller@method');
Route::get('route/{id}', 'Controller@method');

OR you can always add a filter to the first route in order to tell Laravel that {id} should be a number:

Route::get('route/{id}', 'Controller@method')->where('id', '[0-9]+');
Route::get('route/add', 'Controller@method');

This way, Laravel will try to match add to your {id} wildcard but it will fail because add is not a number.

Upvotes: 3

Jasper Poppe
Jasper Poppe

Reputation: 521

Please replace the two lines:

/route/add
/route/{id}

The {id} parameter is not always expecting a numeric value. A string is also valid like "add".

Upvotes: 0

Related Questions