Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone
In this part we will complete REST API we created in last post with actual business logic. (i.e. to get insert, update  and delete categories).

Step 1 – Complete BaseModel

Since we’re planning to work with JSON format, it would be better if we can add some helper methods to the BaseModel class we created in previous posts to give us JSON representation of the model, also a static method to return JSON representation of array of objects. First will add that to BaseModel class. Open ‘lbc/application/models/basemodel.php’ in your editor and add add following code.

toJson() – This will first convert our model to an array of properties and then using json_encode() it will be returned as a JSON string. Eloquent provides to_array() method.

allToJson() – This will accept array of model objects and turn all of them to arrays within one outer array and then do the json_encode() on top of the collection.

A sip of Coffee with Laravel and Backbone - Book

 

Step 2 – Complete API Controller

Next open ‘lbc/application/controllers/api/category.php’ file in your editor and complete each method as below.

get_index() – We check whether we have valid $id and if not will load entire ‘category’ table all() method is provided by Eloquent to load all rows of the table. Once we have all objects call allToJson() method that created in BaseModel to get the JSON representation of it and return. If there is a valid $id use find() method to load the object by primary key field (i.e. ‘id’) and call toJson() method to return JSON representation.

post_index() – This will be called when user sends the POST request with JSON object to save in category table. First thing is to create an object from json input, for that we use ‘json()’ helper method provided by Laravel ‘Input’ class. Once we have it, create new database model to save in the DB and assign corresponding properties, and then call save(). (save() method is given by Eloquent) finally call toJson() in order to return newly created db object to caller, since this has ‘id’ property populated  caller can use it in subsequent calls.

put_index() – This will be called when front end sends us PUT request with JSON model. As before we check ‘category’ table for the given ‘id’ and load it, modify it properties and save back.

delete_index() – This will be called when user sends us DELETE request. We find the object in the table and delete it.

Now we have completed the back end REST API so from next tutorial we can start working on UI with Backbone.js.

Before that will do some testing on these methods to see whether they actually works.

Step 3 – Testing REST API

First will add a new category. Type this in terminal window.

curl -X POST http://lbc.dev/api/category -H “Content-Type: application/json” –d ‘{“code”:”cat1″, “name”:”Category One”}’

REST API - Post Category

REST API – Post Category

Now check GET command to see whether this is saved in DB.

curl -X GET http://lbc.dev/api/category

REST API - Get Category

REST API – Get Category

Now do the update.

curl -X PUT http://lbc.dev/api/category -H “Content-Type: application/json” -d ‘{“id”:”1″,”code”:”catupdate”, “name”:”Category Two”}’

REST API - Put Category

REST API – Put Category

Finally do a delete call and then get call to see it works.

curl -X DELETE http://lbc.dev/api/category/2

REST API - Delete Category

REST API – Delete Category

Posts of this series
Laravel, Backbone.js + Coffeescript Tutorial Part I – Introduction
Laravel, Backbone.js + Coffeescript Tutorial Part II – Setup
Laravel, Backbone.js + Coffeescript Tutorial Part III – Install Laravel
Laravel, Backbone.js + Coffeescript Tutorial Part IV – Database
Laravel, Backbone.js + Coffeescript Tutorial Part V – Data model
Laravel, Backbone.js + Coffeescript Tutorial Part VI – REST API
Laravel, Backbone.js + Coffeescript Tutorial Part VII – REST API
Laravel, Backbone.js + Coffeescript Tutorial Part VIII – Views
Laravel, Backbone.js + Coffeescript Tutorial Part IX – Coffeescript
Laravel, Backbone.js + Coffeescript Tutorial Part X – Backbone
Laravel, Backbone.js + Coffeescript Tutorial Part XI – Backbone
Laravel, Backbone.js + Coffeescript Tutorial Part XII – Backbone
Laravel, Backbone.js + Coffeescript Tutorial Part XIII – Backbone

PS:
As mentioned in the comment below, Use Response::json() or Response::eloquent() to return the json data with proper headers.

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone