Page 1 of 3
Advantages of RESTful URI's
Posted: Thu Nov 05, 2009 5:04 pm
by alex.barylski
I joined a topic over on SitePoint a few days back where Kyber made a remark that totally opened up the idea behind RESTful URI's to -- prior to that I was under the impression that SEF URI's were RESTful -- obviously not the case.
I'm still curious though as to the advantages to both users and or developers to implementing RESTful URI's.
I'm looking to standardize my CMS framework in every way possible, whether that means in W3C validations, meeting standard protocols or standards like RESTful URI's which no CMS I know of has excellent native support for.
Why hasn't this taken off?
Technically speaking it cleans things up a bit for me, reduces the number of lookups my routing system has to evaluate as one segment can now represent several actions (GET, POST, PUT, DELETE). So in that sense it makes sense.
I suppose for other applications, it results in a more portable system as well.
Code: Select all
user/login => Might have meant show the login form
user/login2 => Might have meant execute login request and redirect
Now by simply implying the method in the request, the system just knows what to do, so there is less chance of collision or accidental breaks occuring because the web site changes the URI from
user/login to say user/do-login at a later time down the road.
Anyone else care to chime in with experiences, benefits, side effects, caveats, etc?
So far we have:
- 1. Greater portability/stability in URI scheme
2. Cleaner/less cluttered/convoluted URI namespace
Faster lookups but this might be specific to my framework.
Cheers,
Alex
Re: Advantages of RESTful URI's
Posted: Fri Nov 06, 2009 3:52 am
by matthijs
I really like the ideas of REST. Standardization makes things predictable, which again makes things easier to work with.
The only issue I have difficulty with is how to show the forms needed to insert, edit and delete.
Code: Select all
/users/
shows the users of a system
/users/1/
shows the user 1
A post from a form to add a new user to
/users/
adds a user.
A post with a form with a hidden field "edit" to
/users/1/
edits the user
However, what's left is, how to show the forms? Maybe you could view the forms as a resource as well? Like
/forms/adduser/
/forms/edituser/
Re: Advantages of RESTful URI's
Posted: Fri Nov 06, 2009 11:28 am
by alex.barylski
Niec to hear others are crazy about standards too.
Anyway, not sure if this helps, but as I understand GET is used to show the form, PUT is used to update the data, DELETE to delete and POST to create new records.
So you would request the form using GET but if the request was followed by ID you would pull on the database
Code: Select all
GET user => Show blank form ready to create new (action = POST)
GET user/1 => Show populated form ready for update (action = PUT)
POST => Action to handle creation of a new record
PUT => Action to handle updating of record
DELETE => Action to delete requested resource
http://microformats.org/wiki/rest/urls
Cheers,
Alexs
Re: Advantages of RESTful URI's
Posted: Fri Nov 06, 2009 12:39 pm
by Christopher
REST seems to be one of those ideologies that will not die. I think this is for two reasons: first, the core idea of putting more useful information into the URL makes sense, and second it seems attract the interest of enough programmers who love conceptual stuff like this.
However when you read REST definitions (after all the had waiving is done about using PUT and DELETE, and about resources, etc.) the bottom line is that you are not supposed to use sever side session systems. Now I think that general goal of minimizing session use and pushing easy stuff into the URL makes a lot of sense. But session systems also make some things very easy and clean. And you start to get diminishing returns as you push for stateless purity. So I think the general lack of real-world usage of REST is a practical one.
Re: Advantages of RESTful URI's
Posted: Sun Nov 08, 2009 9:20 pm
by josh
I view REST like a buzz-word, keep all your URLs behind your MVC framework's router and you can change anything from one spot anyways. PUT and GET don't make sense,
I would rather have
/article/publish/1
/article/unpublish/1
/article/view/1
/article/edit/1 (what if your editing isnt your viewing action, what does 'REST' do ).
etc.. I could keep listing possible actions, some of my 'entities' have more than just 4 actions, and I like having easy URLs for them. Of course there is no standard for this because it is specific to your application
Re: Advantages of RESTful URI's
Posted: Mon Nov 09, 2009 2:03 am
by alex.barylski
PUT and GET don't make sense,
Why do you say that? It's entirely tarnsparent to the end user and does simplify things for the developer. Buzzword? Maybe but Roy Fielding is no idiot I am sure if he conceived of the idea, it's probably worth something.
etc.. I could keep listing possible actions, some of my 'entities' have more than just 4 actions, and I like having easy URLs for them. Of course there is no standard for this because it is specific to your application
This is what confuses me as well...I assume that something like 'article/publish' and 'article/update/1' are still considered RESTful URI's
Re: Advantages of RESTful URI's
Posted: Mon Nov 09, 2009 6:24 am
by josh
Why aren't they just URLs? What is different about them that makes them RESTful?
I thought REST meant you had entity/get, entity/post, etc...
Re: Advantages of RESTful URI's
Posted: Mon Nov 09, 2009 7:34 am
by Eran
REST means conveying the state through the URL (as arborint mentioned). It can be any link that uniquely defines a resource or action, without needing additional state stored in other means (cookie / session). The method itself (GET/PUT/DELETE/POST) is communicated via the headers, as with the traditional HTTP methods (GET/POST).
Re: Advantages of RESTful URI's
Posted: Mon Nov 09, 2009 3:33 pm
by Christopher
In my opinion REST is yet another idea ruined by the zealots. I think it is generally a good idea to maintain as much state and you can easily maintain via HTTP. I think that promotes some good practices and has some positive side effects. However, attempting to be a REST purist is a pursuit that quickly results in diminishing returns. There are good reasons why cookies and sessions exist. They make some things much, much easier and cleaner.
I also agree with Josh that the consistency of using the URL instead of adding PUT/DELETE to the mix is better in the long run.
Re: Advantages of RESTful URI's
Posted: Mon Nov 09, 2009 11:10 pm
by Eran
The ZF lead just published a blog post on ZF support for REST. Might be worth a read -
http://weierophinney.net/matthew/archiv ... ework.html
Re: Advantages of RESTful URI's
Posted: Mon Nov 09, 2009 11:27 pm
by alex.barylski
Thanks for the link pytrin.
I'm interested in hearing all opinions, good or bad. Personally I can see where arborint is coming from, it does seem to eventually reach diminishing returns, yet at the same time, it also cleans up the URI space and makes some requests more standard.
I like the idea of having a resource like 'articles' for example be capable of responding via several distinct actions by submitting only the METHOD along with the request.
The methods are standardized (although not natively supported by browsers) PUT, GET, DELETE, POST.
So if I'm building a Windows based application and I configure it to send those methods to 'articles' on my site I need never worry about the URI changing and causing side effects in the Windows application.
For instance, if the URI changed from:
to say
Because there is no standard in this regard (which is what I initially thought RESTful was all about -- but using URI not methods) a simple change like this can break any external tools which reply on the resource, which is why I think RESTful URI's make more sense for web services, rather web URI interfaces -- if distinction makes any sense to anyone -- it does to me
Cheers,
Alex
Re: Advantages of RESTful URI's
Posted: Sun Nov 15, 2009 4:22 am
by matthijs
There are still a few questions nagging me.
1. If in a RESTful app, every URL should be a resource, what about forms? Couldn't you say that a form is just another resource? So you'd have
GET /users/
shows a list of users
GET /users/35/
show the user 35
GET /users/addnew/
shows the form to add a new user
GET /users/edit/35/ or /users/35/edit/
shows the form to edit user 35
2. So if we have the webform to add a user at /users/addnew/, would then the process be:
- GET /users/addnew/
- POST the data to /users/
- after successful insert, redirect to and show /users/36/ ?
3. What about error messages? Something is missing or wrong in the posted data.
- GET /users/addnew/
- POST data to /users/
- redirect to /users/addnew/, and show the error messages next to the form?
Re: Advantages of RESTful URI's
Posted: Sun Nov 15, 2009 7:39 am
by Eran
In my opinion is should be posting to /users/addnew to add a new user. Redirecting for error messages is problematic
Re: Advantages of RESTful URI's
Posted: Sun Nov 15, 2009 2:16 pm
by alex.barylski
Redirecting for error messages is problematic
Why is that? I mean, I agree that forwarding is usually much better for error handling, but messages should be stored in a SESSION regardless, just for this reason, so I am not sure I see your point 110%.
p.s-One more reason RESTful URI's would come in handy, consider a situation I recently came across in development of my CMS framework.
I do not currently implement RESTful URI's although it wouldn't take much effor to switch over. However, I currently have actions mapped similar to this:
Code: Select all
user/add = Show FORM to create new user
user/add2 = Action to process request of creating new user
Very simple and what most people do, but then I entered the latter URI manually by pasting into the URL and hitting enter. Initially everything went fine, despite there being no data in the POST or GET variables from my request object. Then for some *really weird* reason my application chokes with an error ùnknown`with a call stack dump. I spent hours trying to figure out what was going on but for whatever reason when these actions are requested multiple times with GET request methods, they stop working. Something to do with my extended exception not being able to be thrown for unknon reasons.
If each of those action handlers (for lack of a better word) only responded to POST requests having users manually enter the URI and hit enter would be a non-issue as the request would result in the form being displayed.
As it stands (if I determine this to be a bug or not) I will likely have to implement somehting in preDispatch() to check GET and redirect if the action handler URI`s are requested -- a bit of a hack and required by each component I implement. Ugh!
Not sure if that matters to anyone, but it kind of *bugs* me (pun intended)
Cheers,
Alex
Re: Advantages of RESTful URI's
Posted: Sun Nov 15, 2009 3:17 pm
by Eran
but messages should be stored in a SESSION regardless
How come? those messages only apply to the current POST request, why should it persist further?
Also, user input can be used to prefill form inputs, saving the user the trouble of re-entering the information again. Do you persist that to a session as well? why not simply post to the same URI that shows the form?