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

Ligonsker's avatar

Naming conventions from routes to Controller methods

What is a good convention (besides being persistent) for the names of the URLs of the routes and their corresponding controller methods?

I am consistent in the sense that I use snake_case for all:

Route::get('some_url', [SomeController::class, 'some_url_method']);

But should I follow another convention?

0 likes
6 replies
LaryAI's avatar
Level 58

There is no one-size-fits-all answer to this question, as naming conventions can vary depending on personal preference and project requirements. However, here are a few suggestions:

  1. Use RESTful naming conventions: If you're building a RESTful API, it's a good idea to follow RESTful naming conventions for your routes and controller methods. This means using HTTP verbs (GET, POST, PUT, DELETE) to represent CRUD operations, and using plural nouns for resource names. For example:
Route::get('users', [UserController::class, 'index']);
Route::post('users', [UserController::class, 'store']);
Route::get('users/{id}', [UserController::class, 'show']);
Route::put('users/{id}', [UserController::class, 'update']);
Route::delete('users/{id}', [UserController::class, 'destroy']);
  1. Use descriptive names: It's important to use descriptive names for your routes and controller methods, so that other developers (and your future self) can easily understand what they do. Avoid using generic names like "index" or "show", and instead use names that describe the specific functionality of the method. For example:
Route::get('users', [UserController::class, 'listAllUsers']);
Route::get('users/{id}', [UserController::class, 'showUserDetails']);
  1. Be consistent: Whatever naming convention you choose, make sure to be consistent throughout your project. This will make it easier to navigate and maintain your codebase. If you're working on a team, it's a good idea to agree on a naming convention and stick to it.

Here's an example of how you could combine these suggestions:

Route::get('users', [UserController::class, 'listAllUsers']);
Route::post('users', [UserController::class, 'createUser']);
Route::get('users/{id}', [UserController::class, 'showUserDetails']);
Route::put('users/{id}', [UserController::class, 'updateUser']);
Route::delete('users/{id}', [UserController::class, 'deleteUser']);

In this example, we're using RESTful naming conventions, but also using descriptive names for our controller methods. We're also using consistent naming throughout the project.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

This video should be compulsory viewing for every developer imo

"cruddy by design" by Adam Walthan

https://youtu.be/MF0jFKvS4SI

Then you are less likely to need to come up with weird route names in the first place

Personally I never use underscores in routes as they look odd in the browser

3 likes
Ligonsker's avatar

@Snapey thank you! I will then use dashes - instead.

As for the controller methods - I noticed he uses camelCase. do you guys prefer to use camelCase as well for PHP? I got stuck with snake_case because I got used to the native PHP functions using that.

Is it a better idea to switch to camcelCase? i.e. change from

 public function my_controller_method(Request $request) 

to

 public function myControllerMethod(Request $request) 

?

kokoshneta's avatar

@Ligonsker Native procedural functions generally (but not always) use snake_case in PHP; native class methods, on the other hand, generally use camelCase.

Since your controller methods (and almost everything else you do in Laravel, except helpers) are class methods, camelCase is more in line with native PHP naming.

1 like

Please or to participate in this conversation.