Hey everyone! I'm in the process of refactoring a large ERP system, and I want to ensure I'm adhering to best practices for REST API design, particularly concerning the editing permissions between users and admins.
Here's the setup:
- **Backend:** Laravel stateful REST API
- **Frontend:** Separate server on the same domain (using React)
Here's the situation:
- Regular users can edit **their own contact info** through a POST/PUT request to `/users/contact-information`.
- Admins should be able to edit **any user's** contact info, preferably using the same endpoint.
I'm facing a dilemma on how to structure this:
1. Should I add an optional `user_id` parameter to the route `/users/contact-information/{user_id?}` and manage it from there?
2. Create a separate route specifically for admins (like `/admin/users/{id}/contact-information`)?
3. Stick with the same endpoint and determine the action based on the presence of a `user_id` in the request? I'd handle it like this: `$user = $request->query('user_id') ? User::findOrFail($user_id) : $request->user();`
I'd love to hear your thoughts on the cleanest and most scalable solution to this, especially from a RESTful framework and Laravel policy standpoint. Thanks!
2 Answers
If you're using a solid authentication system, there's really no need to pass the user's own ID, as that's typically included in their session data. So, just sticking with `/users/contact-information` for personal edits should be enough.
However, for an admin to modify other users, I’d suggest a clearer URL structure like this:
- **GET** `/admin/users/{user_id}` to get user info
- **DELETE** `/admin/users/{user_id}` to delete a user
- **PATCH** `/admin/users/{user_id}` to update user info
- **POST** `/admin/users` to create a user
This organizes things nicely and keeps it intuitive for API consumers.
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String