The (Many) Benefits of RESTful Development

REST or Representational State Transfer development is web development with controller functions geared around the four HTTP request types (or verbs) – POST, GET, PUT and DELETE, equating these (classically but by no means exclusively) to the CRUD operations Create, Read, Update and Delete respctively. Once you start developing in REST the advantages of this quickly become clear:

  • Restricting your controller functionality in this way naturally prevents you from building bloated controllers
  • It ensures that the layout of your code doesn’t become too obfuscated
  • It allows you to make calls without having to specify your controller action explicitly in the request (as you do in a normal HTTP GET request).

Basically RESTful development is all about nouns, and a complete RESTful design will have the entire functionality of the website broken down into nouns that can be manipulated exclusively by CRUD operations. Each noun will be a controller (and often but by no means always it will also be a database model) and each controller will have up to four functions to represent each CRUD action.

For example take very simple online CMS with a database of users who can log in and change the text on various pages. A RESTful design might look something like this:

Noun – ‘user’
POST – Create new user
GET – Read user details
PUT – Update user
DELETE – Delete user

Noun – ‘page_text’
POST – Create new text
GET – Read text
PUT – Update text
DELETE – Delete text

As you can see, RESTful design where the nouns are models is very very straightforward and logical. But what about allowing the users to log in? We don’t have a ‘login’ model in the same way as the users and page_texts, but we can represent the action in a very similar way:

Noun – ‘login’
POST – Log a user in (create a ‘login’)
DELETE – Log a user out (delete a ‘login’)

And if we wanted the ability to preview new text before saving it:

Noun – ‘preview’
POST – Show (create) a ‘preview’

Now everyone knows how to send GET and POST requests but what about PUT and DELETE? Despite being part of the HTTP protocol since day one they are surprisingly under-used. All modern server-side languages should accept these as valid HTTP methods but in practice the PUT method is rarely used for an update operation as this can be distinguished from a create operation simply by the presence of a key relating to the noun that will be updated:

POST request with no key – Create
GET request with key – Read
POST data with key – Update
DELETE request with key – Delete

It really is that simple. It doesn’t take much thought to break down any operation into one or more nouns with one or more of the CRUD operations being performed on each. Do that and you’ll be programming RESTfully in no time.


There are no comments yet...Kick things off by filling out the form below.

Leave a Comment