Is there a better way to write these routes? It seems I am repeating the same controller in my route files.
Route::post('user', [UserController::class, 'update']);
Route::get('user', [UserController::class, 'index']);
Route::delete('users/{id}',[UserController::class, 'destroy']);
My Answer:
One way to improve the readability and organization of your routes in Laravel is to use resourceful routing. Resourceful routing allows you to define all the routes for a resource (such as a user) in a single line of code.
Here's how you can define resourceful routes for the UserController:
php
Route::resource('users', UserController::class);
This single line of code will generate the following routes for the UserController:
- GET /users - UserController@index
- GET /users/create - UserController@create
- POST /users - UserController@store
- GET /users/{id} - UserController@show
- GET /users/{id}/edit - UserController@edit
- PUT/PATCH /users/{id} - UserController@update
- DELETE /users/{id} - UserController@destroy
This way, you can define all the routes for the UserController in a more concise and organized manner.
Rate this post
4 of 5 based on 5368 votesComments