How to Create Simple REST API in Laravel

Representational State Transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, termed RESTful Web services (RWS), provide interoperability between computer systems on the Internet. (Wikipedia)

This isn’t at all explanatory if you are new to APIs, so I will use a simpler scenario. Think about a Taxi hailing service that requires a map of an entire country; instead of building a mapping service from scratch, it can call the Google Maps API instead. Voila! Problem solved.

Laravel on the other hand is a PHP framework based on the MCV i.e Model, Controller and View.

  1. Model – Deals with interaction with the database.
  2. Controller – Contain the logic that makes the entire application work.
  3. View – As the name implies, contains the code for the visual representation of your application. This is what users interact with via the browser.

In this step by step tutorial, we will not be dealing with views; we will just be thinking about returning a JSON (Javascript Object Notation) response.

JSON (JavaScript Object Notation) is a lightweight data-interchange format.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

STEP 1

The Laravel documentation has a step by step guide on how to setup a new Laravel project , and I advise you to take a few minutes to go through it here, https://laravel.com/docs/5.7/installation or go through this step by step guide of installing Laravel on your local machine.

Laravel comes with its own in-built development server, and you can run the command PHP ARTISAN SERVE to confirm that Laravel was installed properly.

Note: After starting the development server, you have to open a different terminal to run other commands.

STEP 2

This step involves you creating a controller, which will be named myFirstApi, this can be done by running the command, PHP ARTISAN MAKE:CONTROLLER StudentProfileController

You will get a controller created successfully response. It is also important to know the folder structure of a Laravel project, the new controller is the App/Http/Controller/StudentProfileController file.

We will create function and returns an which we will convert to a response in json.

StudentProfileController

STEP 3

Next step is to create a route with which we will access our API response. Open the routes directory. Api.php contains routes for API. Routes defined in the routes/api.php file are nested within a route group by the RouteServiceprovider. Within this group, the /api URI prefix is automatically applied so you do not need to manually apply it to every route in the file. For example the studentprofile will be accessed on the web via the URL http://127.0.0.1:8000/api/studentprofile.

Creating a route in the Api.php file.

Voila!!! You have created your first API.

Of course, APIs can get more complex when they start to involve pulling data from a database, I suggest Bitfumes Tutorial series on APIs on YouTube, if you decide to go deeper into it.

You might also stumble on Lumen, which is a light weight framework strictly made for building APIs.

Data on a web page in a JSON format                              

Related Posts