Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thebigk's avatar
Level 13

Order of routes in web.php

I'm currently on lesson: Laravel 5.4 From Scratch: Rendering Posts.

I made an interesting observation. If the order of routes in my web.php is this:

// Create new post form
Route::get('posts/create', 'PostsController@create');
Route::post('posts/', 'PostsController@store');

// Show individual post
Route::get('/posts/{post}', 'PostsController@show');

..then url: blog.app/posts/create works absolutely fine. However, if I change it to


// Show individual post
Route::get('/posts/{post}', 'PostsController@show');

// Create new post form
Route::get('posts/create', 'PostsController@create');
Route::post('posts/', 'PostsController@store');

then Laravel thinks that I'm trying to fetch results from database with 'id' = 'create' and throws an exception.

It looks like the order of the routes matters. Just out of curiosity, is there any way to handle this (in fairly large projects with multiple routes) ?

0 likes
5 replies
Snapey's avatar

yes,order your routes with the most specific first. Anything with a parameter should be considered a wildcard, matching all routes.

2 likes
CrucialDev's avatar

One option would be to use Route::resource('/posts', 'PostsController');

Assuming you are keeping it a restful controller. Reduces your routes to one line and order is no longer an issue.

rodolz's avatar

Also naming your routes

Route::get('posts/create', [
    'as' => 'create.post',
    'uses' => 'PostController@create'
]);
thebigk's avatar
Level 13

Just read about the resource controllers and that seems to be very useful. They also support 'only' and 'except' so you can use only those methods that you want to use. All good!

Please or to participate in this conversation.