googleads
How To Build And Test Laravel Restful API | Restful API
preloder
API Laravel Web Development

How To Build And Test Laravel Restful API

Restful API is one of the powerful term which help us to use different set of data formats that helps to drive an extensive business. To make a move with that, today we are planning to write on the right way of building and testing Laravel RESTful API. Before that, let’s understand what RESTful APIs are and what its exact impact on web services today.

So basically, what is Restful API?

While REST stands for Representational State Transfer. RESTful API is an application programming interface which relies mainly on stateless protocol like HTTP for interaction. In RESTful APIs, we are using HTTP verbs like GET, POST, PUT and DELETE as actions and endpoints are the resources acted upon.

How To Set Up A Laravel Project

We can use composer to install and handle all the dependencies. First, install Laravel using the following command,

 

Here I am using following command to create a new project using Composer.

 

Since Laravel is already installed, you need to start the server and test to ensure that everything works fine.

 

After starting the server, if you open http://localhost:8000 on the browser, the default page will be loaded.

Tips And Tricks: Best Laravel Security Practices You Must Try It Out!

 

Models And Migrations In Laravel

Before writing scripts for migrations, we have to ensure that you have created database for the application and whether its details are added to the .env file in the root of the project or not and if not please add.

Let’s start with creating model and migration for Product. The product should have name and description. You can use Laravel’s artisan commands to create migrations and models. You can use following command to create the Product model.

 

The -m in the above command is for migration. It tells Artisan to create a migration file for our model. The migration file is as follows,

We can update the up() method by adding two more lines like below.

So its time to run the migration. You can use the following artisan command to run the migration. 

 

After running the migration, by default laravel comes with two migration files create_users_table and create_password_resets_table.

Now we can consider our model Product and add its attributes to $fillable field. The fields inside $fillable property are said to be mass assigned and it will be done using eloquent’s create and update method.

Related: How To Create A Custom Validation Rule In Laravel

 

Database Seeding In Laravel

Database seeding is the process of adding dummy data to the database which we can use for testing purpose. Laravel has a package called Faker, using this we can generate dummy data. Here we are creating two seeder classes. One for Products and other for Users. Use the following artisan command to create the Product seeder class. 

 

Running the above command will result in following class,

 

Similarly create users Seeder class as follows:

 

Then we need to add these two seeders to main DatabaseSeeder class and run

After adding these, if we run the following artisan command,

 

It will successfully run all the seeder classes and it will complete the seeding process too.

Now , let’s create controller and create all the basic functions inside the controller. The controller can be created by following command. 

 

We created controller and filled it with all the basic actions that we required. Now we need to check how the endpoints will looks inside routes/api file. In this case for endpoints we have used implicit route model binding. 

 

API Authentication In Laravel

API authentication in Laravel can be implemented by several ways. Here we are following one simple approach that we have to add an extra field token_api to the users table. For that we need to create migration file as follows.

 

Once we run this following command then automatically the new field will be added,

 

Testing the APIs

Now we can jump into another important part. Testing the API endpoints that we created. There are several tools available to test your API, but Laravel has PHPUnit integration in which they are already set up phpunit.xml. We are following this method. In this case we are using SQLite database which is very fast. But it also has some drawback that, some migration commands may not work but it will not affect us much. So, here our plan is to run migration for each test, so we have to create database for each test and destroy it.
Some configuration change also needs to be done. Inside config/database.php, change the database field in the sqlite configuration to ‘:memory:’.  Similarly we have to enable SQLite in phpunit.xml by adding the following env variable

 

We have to configure the base Testcase class to run the migrations and seed db before doing each test.

The class should be like this

Last but the least! ensure to add the test command to composer.json before running the test.

This will help us to run the test command like below,

 

You can test the Product endpoints as below

   

That’s great! we’re successfully done with building and testing Laravel API, Now what we have seen is the basic and most required steps for creating any Laravel Restful API and eventually will see more on implementing more authenticated techniques like laravel Passport on our next blog. And if you’re the entrepreneur trying to build highly secured site in Laravel or any PHP frameworks then don’t miss the chance to talk with our highly skilled experts who expertise in building highly functional sites. Get in touch with our experts to proof of concept.

The following two tabs change content below.

Nithin Kumar C N

Passionate Senior Software Developer, Expert in PHP, Laravel, JQuery, Angular, With 6 years of experience in web development, he strongly committed himself in delivering authentic applications. Also, he continuously update himself with new technologies & features which drives him to come up with blogs on unique topic.

Leave a Reply

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

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call