Casey
Casey

Reputation: 560

Laravel route conflict with parameter

I'm trying to setup routes in Laravel using

Route::get('/post/{id}', 'PostController@index');
Route::get('/post/new', 'PostController@create');

But when I go to mysite.com/post/new its runs the index function thinking its an {id}.

So I'm wondering if I can force /new to go to the create function or if I have to change /post/ to something different.

Thanks in advance for the help!

Upvotes: 5

Views: 3687

Answers (2)

Sand Of Vega
Sand Of Vega

Reputation: 2416

Route::get('/post/{id}', 'PostController@index')->where('id', '[0-9]+');

Take a look: Regular Expression Constraints

Upvotes: 14

Dionisis K
Dionisis K

Reputation: 644

Also important !! .The order of route declaration matters. Try this

Route::get('/post/new', 'PostController@create');
Route::get('/post/{id}', 'PostController@index');

and you ll notice that your app is able to identify new as a different route from {id}.
That happens because route resolver searches until it finds the first pattern matching the route

Upvotes: 14

Related Questions