Laravel

How to create a simple rest API in Laravel ?

In this tutorial we will learn that how to create a simple rest API in Laravel. We will not deep dive in this topic. We just learn the basic concepts and basic steps of API in Laravel.

Prerequisite

You need to install PHP and Laravel in your laptop. To ensure that these two things are installed in your laptop, you can run these commands in your terminal. To check PHP installation just run the command php. For making sure Laravel installation you need to run Laravel command. You will see output like this

Rest API in laravel
Check Laravel Installation

Create new Laravel project for API

After installation of php and Laravel, Next step is to create a new Laravel project. This is quite simple with Laravel. Laravel new command will do this for you. Open your terminal at folder, where you want to create your project. In terminal just run the command Laravel new demoapi . Here Laravel new is a command which will create your project with all required libraries already installed. And the demoapi will be your project name. You can change this name as per your requirement. After running this command system will process this command and will take some time to create project.

Create First Laravel Rest API

After creating the new project, this is the time to build our first Laravel rest API. Open your project in any editor, sublime, vs code or any other which is easy for you. To create an API, we have to do two things. First we will create a controller and second we will add route for our API endpoint.

Create controller in Laravel

To create controller class in Laravel, we will use the command line interface. Open you terminal and run the command php artisan mak:controller dummyController . This command will create a controller in Http -> Controllers directory with name dummyController.

Now open dummyController and create a function in it. This function will return the output data in json format, which is required for us as API response. Let’s name our function getData, and return json from in it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class dummyController extends Controller
{   
    function getData(){
        return["namee"=>'john', "email"=>'john@gmail.com']; 
    }
}

Here we create a function getData() which is returning a json object with two properties (name and email). Now we have to create a route and set this function as a source of data for that route.

Add Route for Laravel rest API

For web applications we will create routes in routes->web.php file but for API we will create routes in routes->api.php file.  Now open your api.php file and create a route. This route will be the endpoint of you rest API. In api.php you will see the boilerplate code. This code is for API authentication. We will learn API authentication later, but now this code is not for us.

To create a route, just import the controller call at top of the file and paste below code in your api.php file. Now your api.php file will look like this

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\dummyController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});
Route::get("data",[dummyController::class,'getData']);

Here we create a route. Method of this route is ‘get’. There are 5 types of methods in rest API request (POST, PATCH, PUT, GET, DELETE). Get is used for retrieving the data from http request. So we will discuss here only the get method. After defining the type of our endpoint, we name our endpoint as ‘data’. Next we write our controller name and write the source function ‘getData’.

We have done our work. Our API is ready for use. Our API endpoint is ‘data’. We will hit this endpoint and will get result from getData function, which we have created in our controller class. Let’s test our API on browser or postman.

Test Laravel API

We have created our first API in Laravel. Now we will test it. The GET request can be tested on both ( browser and postman ).  To test API, first we have to run the PHP default server using the command. Run the below command in you terminal.

Php artisan server

This command will run the PHP default server. On which we can test our API endpoint. Now we are ready to hit our API endpoint. Just hit this URL in browser or postman. And you will see output as json object.

OUTPUT
Test Rest API in laravel
Laravel rest API testing

Conclusion

In this tutorial we have learned the basic concept of rest API in Laravel, without deep dive in to advance things. We keep our tutorial as simple as we can, so that you can understand well. Hope you understand well and will be able to create your own APIs. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *