RESTful API implementation with Laravel 5

Share Blogs

Posted by admin on June 17, 2016

Laravel framework has recently gained a lot of popularity and because of all the good reasons. It recently launched a new major release 5.x

We quickly tried our hand at creating a RESTful API using the new a version. Did hit a few roadblocks but in the end it all turned out to be a good learning experience.

Sharing the steps to create a RESTful API with laravel.

You can download the zip file with db and other files created for this project. Once you have installed laravel, you can simply put the files in framework and this API, as described, should work for you.

Getting started:

- To get started with Laravel you need to install composer

- Second step would be to install Laravel framework itself using Composer

- After you have downloaded and setup laravel you can run the command "laravel new restful" to create the project (named restful)

- Create a DB in MySQL

- modify the configuration file restful/config/database.php to point to correct DB settings.

Now getting to interesting part:

Create Model

For creating model class you can run the command
>php artisan make:model Schools
This will create a model file named as "Schools.php" with class name "Schools". Remember this only creates a stub for class.
You can open the file in your editor and add following text in the same

protected $table = 'schools'; // add the table name schools in our case
// Fillable data in table : Use the column name in which data will be fillable
protected $fillable = ['name', 'address','type','website'];
//column name that will not be shown
protected $hidden = ['type'];

Create Controller

Run the following command to create stub for controller class.
>php artisan make:controller SchoolsController
This will create "app/http/controller/SchoolsController.php" file with class named "SchoolsController". The stub also contains basic functions declared that generally the controllers need to have. Basic functions are:
- index() - for displaying list of the resource
- create() - this displays the form for creating the school resource
- store() - this is for the submit request of the form to create a school
- show($id) - this displays a single school
- edit($id) - display edit form a single school
- update($id) - update a school
- destroy($id) - delete the school record

Since we are only interested in creating a Create, Retrieve, Update, Delete (CRUD) RESTful service, we do not need to add any new functions on the class.

Create Transformation

Laravel framework allows you to create transformation?classes to hide OR show selected data in the json response data according to need.
To transform the JSON, create a Transformers folder in restful/app directory and create two php file name as Transformer.php and SchoolTransformer.php. The files exist in the download zip file.

Create Routing

Laravel framework comes with powerful way to create routes. For our purpose, we can define a route group for API as displayed below

Route::group ( [ ]function ( );

Route::resource('schools', 'SchoolsController' );
❵ );

This will create routes for all the functions in the controller as displayed in the below image


As we all know, mapping for HTTP verbs for RESTful applications is defined as displayed below.


So, as we can see, by default laravel generates routes based on restful principals which is a good thing. Now we have just a couple more things to do and we would be done. We know by experience that APIs do change and we need to come up with newer versions of the same. So it is a good practice to have version numbers embedded in the API endpoint. Hence we modify the above code to add parameter ['prefix' => '/api/v1'], with this our API have uri like /api/v1/schools


One more thing that you may want to add to your APIs would be authentication basic. That would also be a simple change. All you need to do is add another variable to the parameter 'middleware'=>'auth.basic'.

So as you can see, creating RESTful API with Laravel is relatively straight forward process since Laravel is designed to support RESTful paradigm.

If you need any help with your laravel projects, feel free to contact us

Share Blogs

Want to discuss your idea or project with us

Project Inquiry

Interested to Join Us?

Post Your Resume