问题
In my web app I've got a user
model and a journal
and post
model. Each user can have multiple journals, each journal can have multiple posts. Is below the best way to represent this in a RESTful way?
/profiles/<username>
/profiles/<username>/journals/<journal_id>
/profiles/<username>/journals/<journal_id>/posts/<post_id>
or would:
/profiles/<username>
/journals/<journal_id>
be a better way to go?
回答1:
You have three types of resources: Profiles, Journals, and Posts.
If your business needs is to allow end-users to have access to them all, you will need to provide PUT/POST/GET/DELETE CRUD operations on these resources base on your needs (you may not need all operations on all resources thought)
/profiles/<username>/
/journals/<journal_id>/
/posts/<post_id>/
If you need to provide a logical mapping and relationship between these resources, you will also need to provide the following:
/profiles/<username>/journals/<journal_id>
/profiles/<username>/journals/<journal_id>/posts/<post_id>
You can also look into this posting as what may be the approach to think about business relationship mapping between resources.
来源:https://stackoverflow.com/questions/19033462/simple-rest-url-scheme