Reputation: 560
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
Reputation: 2416
Route::get('/post/{id}', 'PostController@index')->where('id', '[0-9]+');
Take a look: Regular Expression Constraints
Upvotes: 14
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