SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question pitfalls in RESTful architecture

    Hi everyone,

    As most of you will know, REST is an architectural style for developing applications. I find it most interesting, and I'm thinking about trying to to implement it in the future. Kyberfabrikken has got to me with his RESTful framework Konstrukt, and it really seems like a good and organised way of working.

    There is only one doubt in my mind and I would like to hear your opinions on the take. Taking an example of an Admin user, that GETS a usercollection, a list of current users is shown. Now, what if that Admin user want to remove multiple Users from that Collection? You can't do a DELETE on the Collection because that should remove the Resource, not a subset of the Collection, but I reckon it quickly becomes tedious to remove every single Resource with a separate request.

    Anyway, that's one of my main concerns. Are there any other pittfals in "loosely" adhering what REST dictates? I'm saying loosely here, 'cause I can't rule out the fact that an online application may have state in one way or another.

  2. #2
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see a problem with having resources like:
    /users - list users
    /users/delete?id[]=23&id[]=24&id[]=25 - delete a collection (parameters should be posted)
    /users/23 -show user with id 23
    /users/23/delete - delete that user

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doing multiple requests to remove or update a collection is not only tedious but it has quite a bit of overhead. on the other hand, you have more access to the progress of your operation...if you're updating 100 records that each take 1 second, you can show the user the progress.

    to answer the question tho, i don't see any pitfalls to loosely adhering to REST. if there's a request that you need to make an exception for (such as deleting a collection), do it. wysiwg's example of deleting is ok in my book...while it doesn't exactly match up, it works.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the REST world, every url is a resource, so a single user could be a resource, but so could a collection of users. In principle, there is no problem in issuing a DELETE command to a collection, just as you could to a single instance. The only pitfall is that you probably wouldn't want to issue an instruction with side effects to a url that is partially defined by application state. Eg. it would be a bad idea to DELETE on a url like /users?offset=40&limit=10 because the exact items of that collection isn't necessarily the same for each request. So you would probably rather send a DELETE to /users?id=40,41,42,43,44,45,46,47,48,49 as this would be defined on itself. The metric should be that if you issue the same DELETE request multiple times, you'll only ever get the instruction executed once.

    One way to look at it is that /users?offset=40&limit=10 and /users?id=40,41,42,43,44,45,46,47,48,49 are two separate resources. While they may currently represent the same information, that is a temporal coincidence. The first is a search, while the latter is a particular collection of items.

  5. #5
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    One way to look at it is that /users?offset=40&limit=10 and /users?id=40,41,42,43,44,45,46,47,48,49 are two separate resources. While they may currently represent the same information, that is a temporal coincidence. The first is a search, while the latter is a particular collection of items.
    Alright, that makes quite a lot of sense. While we're on the subject (I'm still exploring REST ), I'm curious how to handle relationships between different resources. For example, one could have a resource which is an article. The specific resource is located at /articles/learn-c-plus-plus-in-21-days (heh). Now, I'd imagine there really isn't anything wrong in showing the comments on that article on the same page.

    However, I am curious about how one would go about posting a comment on the article. I'd image POST /articles/learn-c-plus-plus-in-21-days/comments/ would be enough, yes? Or am I totally off base here? Also, if this actually is the preferred way of managing relationships like this, it there a way in Konstrukt to know the name of the previous controller in the chain has?
    Last edited by webaddictz; Jan 9, 2009 at 06:47. Reason: edit: geesh, my english is bad sometimes :)

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webaddictz View Post
    However, I am curious about how one would go about posting a comment on the article. I'd image POST /articles/learn-c-plus-plus-in-21-days/comments/ would be enough, yes? Or am I totally off base here?
    There aren't any definitive rules about this, within REST, but I'd say the way you described is a pretty common way to do it. Take a look at how Rails default mapping of resources looks.

    Quote Originally Posted by webaddictz View Post
    Also, if this actually is the preferred way of managing relationships like this, it there a way in Konstrukt to know the name of the previous controller in the chain has?
    If I understand your question correctly, then this is why the url method is an instance method on the controller. Assume that you have an ArticlesListController to render the url /articles/, and an ArticleShowController, which renders /articles/learn-c-plus-plus-in-21-days. Withint the ArticleShowController a call to $this->url('comments'), would generate the url /articles/learn-c-plus-plus-in-21-days/comments.

  7. #7
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    There aren't any definitive rules about this, within REST, but I'd say the way you described is a pretty common way to do it. Take a look at how Rails default mapping of resources looks.
    Thanks for the link, I'll look into that.

    Quote Originally Posted by kyberfabrikken View Post
    If I understand your question correctly, then this is why the url method is an instance method on the controller. Assume that you have an ArticlesListController to render the url /articles/, and an ArticleShowController, which renders /articles/learn-c-plus-plus-in-21-days. Withint the ArticleShowController a call to $this->url('comments'), would generate the url /articles/learn-c-plus-plus-in-21-days/comments.
    Then I'm afraid my question isn't clear enough You've got the ArticleViewController, but also a ArticleCommentsController (right?). So if the url would be 'articles/name-of-article/comments/', my question is: is it possible to find 'name-of-article' from within ArticleCommentsController?

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From the ArticleCommentsController, you can call:
    $this->context
    which is the ArticleViewController. When you call:
    $this->context->name, you'd get "name-of-article". In the new version coming out soon, you'd be calling $this->context->name().

  9. #9
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webaddictz View Post
    Then I'm afraid my question isn't clear enough You've got the ArticleViewController, but also a ArticleCommentsController (right?). So if the url would be 'articles/name-of-article/comments/', my question is: is it possible to find 'name-of-article' from within ArticleCommentsController?
    You can call $this->context->name. Usually you don't really want the name, but rather some kind of model layer component. So I would implement a method getArticle() on ArticleViewController, and then call it from ArticleCommentsController.

  10. #10
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    So I would implement a method getArticle() on ArticleViewController, and then call it from ArticleCommentsController.
    Ah, that's actually a better solution, as it promotes reuse. Seems obvious if you think about it. Thanks!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •