Farshad's avatar

Manage Patch routes

I have some route like below for example

Route::patch('/users/{id}/update-name', 'UserController@update_name')
Route::patch('/users/{id}/update-password', 'UserController@update_password')

I need to find is there any good structure for manage Route like this to stop write action in the url like this

Route::patch('/users/{id}', 'UserController@update_name')
Route::patch('/users/{id}', 'UserController@update_password')
0 likes
3 replies
Nakov's avatar

You have to have at least something different in order to differentiate them, otherwise it will always execute the latest route. So either the http method should be different or if they both accept a parameter which is not id for both so you can match it using regular expressions.

Maybe a patch request to /users/{id}/name is a better endpoint then update-name. Just a suggestion.

Farshad's avatar

@NAKOV - it doesn't make any different to use this /users/{id}/name. I want not use action in the url or find way to manage this in the controller or maybe laravel suggest one way for manage that

Nakov's avatar
Nakov
Best Answer
Level 73

I know. But as I said it will overlap having completely the same route twice. You can handle it in a single method on your controller. So instead of methods like you have, put a simple update method in your controller and handle the update there based on what field has been changed.

1 like

Please or to participate in this conversation.