Building RESTful API in Laravel – start here

Over the past few weeks I’ve been very busy learning basics of Backbone – an MV* framework for Javascript because it simplifies creation of one page applications (great for mobile devices!). In just two weeks I built a working mobile app for the company I’m working for, and soon it will be released to Android and Apple app stores. Of course there is great complexity behind this all but what I have noticed is that having a bad API for your application can slow down building mobile apps and impact the ecosystem of your product. My goal for this month is to learn building RESTful APIs from scratch in Laravel – the framework that I use for all of my new web applications. If you don’t know what RESTful means, check out this wikipedia article : http://en.wikipedia.org/wiki/REST.

What is the purpose of RESTful APIs? Check out the slides below :

(from http://blog.apigee.com/detail/restful_api_design)

Or watch this video on vimeo :

Teach a Dog to REST from Apigee on Vimeo.

Basically for all new and old web applications it makes sense to do RESTful APIs instead of custom schemes that don’t scale and are very hard to comprehend.

I am using Laravel, so where do I start?
To begin learning RESTful API design I first watched this great video tutorial by Jeffrey Way:

Then I started to think how I would incorporate this in my applications and after researching lots of forum posts on Laravel.com I came to conclusion that to start with, all I need is one route and one controller for a single collection of data.

Let’s say I want to make a simple Todo list application, according to the REST architecture, I would have the following structure:

GET /todos – show all todos

GET /todos/id  – show todo with ID equal to id

POST /todos – create a new todo

PUT /todos/ – update todo with ID passed from the application

DELETE /todos/id – delete todo with ID equal to id

Well, as for the route, we can start with something very simple :

In Routes.php I just add :

Route::any('api/v1/todos/(:num?)', array('as' => 'api.todos', 'uses' => 'api.todos@index'));

And for the controller I start with this structure (it is in application/controllers/api/todos.php):

<?php

class Api_Todos_Controller extends Base_Controller {

	public $restful = true;

	public function get_index($id = null) 
	{

	}

	public function post_index() 
	{

	}

	public function put_index() 
	{

	}

	public function delete_index($id = null) 
	{

	} 

}

?>

This is simple enough to get me started and this is enough to be considered RESTful if we implement necessary actions upon our model.

In the next blog post I will go through a real example of data manipulation from DB and will show how to return some beautiful JSON data to the user!

Stay tuned, follow me on Twitter!

Update! Next part of the tutorial is published:
Building RESTful API in Laravel – part 2 – Design API Controller

Further resources:

http://blog.apigee.com/detail/restful_api_design – Designing RESTful APIs

http://technetlk.blogspot.com.au/2012/09/laravel-backbonejs-coffeescript.html – Backbonejs and Laravel

Liked it? Take a second to support Maks Surguy on Patreon!
Become a patron at Patreon!

You may also like

16 comments

Leave a comment