Home  >  Q&A  >  body text

php - What to do about duplicate naming of routes in restful form?

For example, this route
GET /users/{id}/comments
Now let’s say there is a message board on the user’s page, and we plan to use a comment component
For replies posted by users in some posts, use This is also the comment component
When designing the database, these two comment components use the same data table (polymorphic association) (you don’t have to worry about this)
The point is that this route has conflicting meanings:
Is it displaying the user message board (posted by others)?
Or is it displaying the user's replies in the post?

So, how to solve this problem and distinguish these two routes in the form of restful

Sorry, I am a Virgo,,,(but I also want to find the best solution to this problem)

Actually, there is another scenario for this problem, which is how to distinguish between those who follow me and those who follow me.
GET /users/{id}/follows
Does this route show those I follow or those who follow me?

黄舟黄舟2700 days ago866

reply all(3)I'll reply

  • 为情所困

    为情所困2017-06-20 10:09:20

    For your first scenario, I think there must be a field in the data table to identify whether the user's message board or post is in the user's message board. This field is temporarily called type, so it can be written as GET /users/ {id}/comments/{type}

    In another scenario, you can use the parameter type to specify whether you are following me or following me. You can write GET /users/{id}/follows/{type}
    Of course there is another way to follow me. of and those who follow me separately

    GET /users/{id}/follows -> 我关注的
    GET /users/{id}/followers -> 关注我的

    reply
    0
  • 学习ing

    学习ing2017-06-20 10:09:20

    Leaving a message to a user and leaving a message to a post operate two different resources. Resources are located by URI. Different resources use different URIs. It is wrong for you to use the same URI to operate different resources.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-20 10:09:20

    You can use message on the message board on the user page
    followers are following me
    following is what I am following
    Just give me a few more English words.

    I don’t think the questioner seems to have changed his name. In fact, the first reason for changing the name is that it can fundamentally solve the problem; the second reason is that it is convenient for subsequent maintainers. Using confusing names will cause problems sooner or later.

    reply
    0
  • Cancelreply