Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 952
Casey Avatar asked Nov 29 '22 08:11

Casey


1 Answers

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

like image 152
Dionisis K Avatar answered Dec 05 '22 22:12

Dionisis K