Search code examples
phpapiresturiobject-relational-model

RESTful API resource architecture and syntax; Am I doing it right?


I'm working on implementing a RESTful API resource architecture in my micro-framework. I'm modeling my resource paths in parallel with the object hierarchies, taking advantage of the fancy Routing functionality I've recently developed. (Oh, it's fancy!)

The URI syntax I'm using is:

'http:// www.site.com / resource [ ; key1 = param1 [ & key2 = param2 ] ]'

This would permit such URIs as:

'http://www.site.com/user;id=123/article;id=456'

Which maps to something like (once I incorporate this functionality into the router):

$user->getByParams('id=123')->article->getByParams('id=456');

So further examples:

'GET /user;id=123 GET'
    # read User object data matching id=123

'GET /user;id=123/article;id=456'
    # read Article object data matching id=456
    # belonging to User object matching id=123

'GET /user;name=john&age=20/article;title=hello%20world'
    # read Article object(s) data matching title='hello world'
    # belonging to User object(s) matching name='john' and age=20

The issue I'm having is with the last one, matching multiple results against query data (a' la LIKE in SQL) What would be a safe character to signify a wildcard? Asterisk (*) appears on the sub delimiter reserved list here http://labs.apache.org/webarch/uri/rfc/rfc3986.html#reserved, so it would remain un-encoded. Perhaps it would be easier to use %25 (%)

I understand this issue could be implementation specific, however are there any existing frameworks that implement a RESTful API resource architecture similar to this, that I could reference for ideas?

Also, am I going against the grain entirely here?


Solution

  • Currently the best resource on how to construct URI templates with parameters is here.

    Having said that, you should really avoid thinking about URI design when trying to do resource design. I try and design resources by drawing blocks, naming them, and then drawing arrows to represent the links that connect the resources. This is similar to the way you would go about designing a web site.

    Then, once your resources and links are identified you can create whatever URI scheme is the easiest for your server framework to parse.