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 try to start with REST API that need to do CRUD operations for ‘category‘ model and also how to setup proper routes to access them. I will implement REST API to work with JSON messages so that it will be easier for Backbone models and collections to work with.Before we start, it is good to have Laravel configured to work with clean URLs. What I meant by clean URL is, instead of make user to access URLs such as ‘http://lbc.dev/index.php/docs’ how about setting that as ‘http://lbc.dev/docs/ which is much easier to remember and less typing. Let’s do it first.

Step 1 – Configuring Clean URLs

A sip of Coffee with Laravel and Backbone - Book

 

Open the browser and type http://lbc/dev/ and click on ‘wonderful documentation‘ link. It will navigate to Laravel documentation and when you look at browser URL box it is shown as ‘http://lbc.dev/index.php/docs’
First thing to do now is to enable url rewrite in Apache2, you can simply do it by issuing following command in terminal window.

sudo a2enmod rewrite

and then restart Apache2

Then open lbc/application/config/application.php file in your editor and goto place where it has ‘index’ => ‘index.php’ and remove index.php, so it will be as ‘index’ => ”, and then save

That’s it, now open your browser and go to ‘http://lbc.dev’ and then click on same link as below and now you will be redirected to ‘http://lbc.dev/doc’ which is same place as before but with clean URL.

Step 2 – REST API Controller

Then we will add a controller to handle REST requests, I need to group these API related controllers into a different folder than normal controllers which renders views to the UI. For that I will create new folder called ‘api‘ under lbc/application/controllers/.

Now under this new folder add new PHP file and name it category.php. Open it in your editor and write following code.

Here we have to follow this naming convention i.e. in Api_Category_Controller

Api_ inform Laravel framework it is inside ‘controller/api‘ folder and then _Controller to indicate that this is a controller class. _Category is the name of our controller. We have to follow this convention since Laravel is a framework . (i.e. the difference between framework and library where when we use a Framework we have to follow it conventions, but we can use libraries as we want)

Next point is we have extended our controller from Base_Controller, similar to when we define Models, this is a good practice so that we can put common functionality into base controller and reuse them.

Then in 6th line specify, we hope to implement REST request handling in this controller, so only methods(actions) we will provide to outside is get_, post_, put_ and delete_

I also made slight modification to get_index so that if user sends us $id value, we only return category which has that id or else we return all categories.

For now I will just output some string in all methods so that we can test whether it works properly.

Step 3 – Define Routes

Now we have to tell Laravel route engine how to find this controller and what url format routed in to this controller.

For that we need to add route entry into lbc/application/routes.php file. Open this file in editor and just after  <?php line enter following code.

Let’s explain what each part of this does.

Route:any() – Here we say we’re going to add new url route entry which should handle all HTTP requests, i.e. GET, POST, PUT and DELETE (i.e why any())

‘api/category/(:num)?’  – This is what user will enter on browser or simply the URL user will access in order to get served by our controller. However (:num)? means, as the last part of the url user could either send a number or he can ignore that part. (:num indicates we expect numeric value and ? indicates optional)

‘as’ => ‘api.category’ this is for our reference so that we can use ‘api.category’ to refer this url or emit this url in rest of the application. (Don’t worry about this since it doesn’t affect our implementation)

‘uses’ => ‘api.category@index’ – This is how we tell Laravel route engine where to route this request. In this case send it to Api_Category controller and will be served by corresponding ‘index‘ methods based on the type of the HTTP request (i.e GET by get_index, PUT by put_index and so on)

Ok now basic implementation of REST API controller is done. We can now open up a browser and type http://lbc.dev/api/category/ and see whether it prints ‘Get’, also if we access http://lbc.dev/api/category/10 it should display ‘Get 10′

Access REST API

Access REST API

in order to test other HTTP methods you can type following commands in terminal window.

curl -X POST http://lbc.dev/api/category
curl -X PUT http://lbc.dev/api/category
curl -X DELETE http://lbc.dev/api/category/10

In next tutorial will see how to complete our controller to work with Category model.

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

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