Lately I’ve been re-reading full Laravel documentation and found quite a lot of “small details” which no one actually noticed in tutorials or books. One of these is using “-” symbol in route parameters.
Let me show you what I mean:
This is correct way of using parameter:
Route::get('posts/{post}', function ($postId) { // });
This is incorrect way of using parameter:
Route::get('posts/{post-id}', function ($postId) { // });
Apparently, you cannot use “-” in parameter, like post-id here. Instead, you can use underscore, like this:
Route::get('posts/{post_id}', function ($postId) { // });
Probably you’re wondering why I’m telling you this? It’s such a small detail!
The thing is if you do use “-” symbol, you won’t receive any visible error, that route just won’t work and you will probably receive 404 page. And you will be wondering what the hell is wrong in this world.
And yes, it’s mentioned in the official documentation. Hope that helps!
I had this problem once and took me ages to work out that the ‘-‘ was causing the issue… 🙁
This makes perfect sense to me why it doesn’t work, using hyphens generally will break matcher routing.
Just follow the PSR rules of camelCase for variables and you’re good (a route parameter is just a variable at the end of the day)
This not so annoying as a difference between “/search” and “/search/”. This is really hard to find
I believe it’s recommendated from Laravel’s doc that:
Route::get(‘posts/{id}’, function ($postId) {
});