Route parameters – no hyphen, only underscore

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!

Like our articles?
Check out our Laravel online courses!

3 COMMENTS

  1. 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)

LEAVE A REPLY

Please enter your comment!
Please enter your name here