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

crwh05's avatar

Naming URI resources with two words

I have a resource called ip_address (table = 'ip_addresses', model = 'IpAddress'). What should I call it in my routes?

Something like 'card' is simple

Route::get('cards/{card}',  'CardsController@show');

But how should I name ip_address for routes?

Route::get('ip_addresses/{ip_address}',  'IpAddressesController@show');
or
Route::get('ip-addresses/{ip-address}',  'IpAddressesController@show');
or
Route::get('ipaddresses/{ipaddress}',  'IpAddressesController@show');
or
Route::get('ipAddresses/{ipAddress}',  'IpAddressesController@show');

Or some combinations of these? I don't want it to interfere with any other Laravel functions like implicit binding or just generally being out of sync with the rest of the project naming conventions.

I would rename it to 'ip' but I will encounter this problem in the future again with other such two word resources like 'joint_owner'

Any help/advice much appreciated

0 likes
11 replies
d3xt3r's avatar

As far as my knowledge goes, nothing is assumed by laravel with regards to route parameters (at-least as of now). For implicit route binding all you need to make sure is that the parameter name is same as variable being accessed.

For me, I use the same name as the variable(db field) i am referring to when accessed through controller, and the class name when used in implicit route binding.

martinbean's avatar

@crwh05 Use dashes in the URI pattern but camelCase for the route parameter, as that is what Laravel will look for if trying to do route–model binding. So:

Route::get('ip-addresses/{ipAddress}', 'IpAddressController@index');

You’ll see I also favour the singular for controller names.

4 likes
crwh05's avatar

Thank you @martinbean , dashes look good in the URI alright. When it comes to resource routes they automatically take the URI name as the route parameter which would mean I would end up with a dash divided parameter. Or can you define it? Is something like this valid?

Route::resource('ip-addresses/{ipAddress}', 'IpAddressesController');

I have been naming my controllers plural as Jeff does. But when it comes to words that aren't as simple as add an 's' it does worry me that maybe a singular word would prevent errors being made in future. Especially if someone with english as a second language is working on the project.

1 like
Citizen's avatar

@crwh05 Did you find a solution to your problem? We ran into this as well.

curtisblackwell's avatar

I just figured out my problem.

I used artisan make:model -mcr ProductionOrder and Route::resource('production-orders', 'ProductionOrderController');.

The generated controller had this method: public function show(ProductionOrder $productionOrder), but by dd(Route::getRoutes());ing, I saw that Laravel defines routes like this:

allRoutes: array:8 [
    "HEADproduction-orders" => Illuminate\Routing\Route {#240}
    "HEADproduction-orders/create" => Illuminate\Routing\Route {#239}
    "POSTproduction-orders" => Illuminate\Routing\Route {#238}
    "HEADproduction-orders/{production_order}" => Illuminate\Routing\Route {#237}
    "HEADproduction-orders/{production_order}/edit" => Illuminate\Routing\Route {#236}
    "PATCHproduction-orders/{production_order}" => Illuminate\Routing\Route {#235}
    "DELETEproduction-orders/{production_order}" => Illuminate\Routing\Route {#234}
  ]

So I had to update the controller method to public function show(ProductionOrder $production_order).

crwh05's avatar

I have since moved on from this project but looking back through my code I see I just ended up avoiding double barrel names.

Route::put('ips/{ip}', 'IpController@update');

Perhaps there are cases when you have to have double barrel names but often there are ways you can avoid it. like by breaking it up. e.g. production order is a type of order called production.

Just a thought. I'm not overly happy with it but it did the job at the time. I would like to hear what works for you all.

@curtisblackwell I would avoid using the variable name $production_order as it is not the correct case (lowerCamelCase) defined in the PSR coding standards which Laravel uses.

1 like
jason-mccreary's avatar

As coolsam said, this is an old thread. But I stumbled upon this and felt the original question wasn't properly answered. So for future readers wondering about naming URI resources with multiple words here's a fuller answer:

By default, modern versions of Laravel (6+) use snake case for the route parameter names. You may implicitly bind a multi-word model with the type-hint and parameter name using either snake_case or camelCase. By Laravel (and PHP) convention, a camelCase name would be preferred.

So, for example, a MultiWord model and corresponding resourceful controller of MultiWordController would register a resource route as:

Route::resource('multi-word', MultiWordController::class);

By default, this would produce the following URIs:

multi-word.index   | multi-word
multi-word.create  | multi-word/create
multi-word.store   | multi-word
multi-word.show    | multi-word/{multi_word}
multi-word.edit    | multi-word/{multi_word}/edit
multi-word.update  | multi-word/{multi_word}
multi-word.destroy | multi-word/{multi_word}

The action, for edit, using implicit model binding may be:

public function edit(MultiWord $multiWord) {}

Or, if you prefer:

public function edit(MultiWord $multi_word) {}

This is how Laravel handles naming for multi-word resources. Any other naming would require Naming Resource Parameters, as coolsam noted.

6 likes

Please or to participate in this conversation.