Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building and Testing RESTful APIs With Laravel

DZone's Guide to

Building and Testing RESTful APIs With Laravel

Look at a handy tutorial for building and testing RESTful APIs with Laravel.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Experts across the globe agree that a RESTful API is among the best options available in the market for developing an interactive interface for communication between a company's data and audience. Moreover, what better framework to build RESTful APIs than Laravel. Created by Taylor Otwell, the Laravel open-source framework aims to improve the developer's efficacy by prioritizing convention over configuration. It also comes integrated with a plethora of unmatched features and novelties such as real-time communication, API authentication, job queues, and more.

Now, before we delve into the nitty-gritty of building a RESTful API with Laravel, let's quickly go through some basic facts about the latter.

REST a.k.a. Representational State Transfer refers to a programming standard for communication between applications via a stateless protocol. In RESTful APIs, resources designate endpoints while HTTP methods denote actions.

Here's a list of the actions for initiation with their mode of access:

  1. GET: Fetch resource

  2. POST: Build a new resource

  3. PUT: Update resource

  4. DELETE: Delete resource

PUT vs POST

Between POST, PUT, and sometimes even PATCH, developers are usually divided over which action to use for data storage, no matter if it is for creating a new resource or updating an existing one. Hence, to avoid any confusion, we have used PUT in this tutorial. One must remember that one of PUT's characteristic feature is its idempotence. It doesn’t matter how many requests you send using the same data; it will result in only one change.

Configuring the Project 

The Laravel framework needs Composer to install and manage dependencies. Now, to install Laravel use the following command:

$ composer global require laravel/installer

Once the installation is complete, scaffold a new application in the following manner:

$ laravel new myapp

However, for this command to work, you must have ~/composer/vendor/bin in your $PATH. You can also bypass that by creating a new project with Composer. For that, use the following command:

$ composer create-project --prefer-dist laravel/laravel myapp

Moreover, after this start with the server and see if everything is working as it should with the following command:

$ php artisan serve

Laravel development server started: <http://127.0.0.1:8000>

Listed below are some of the pointers to help you with the project requirement:

  • Database seeding: It refers to populating a database or even a database table in relational systems with test data, which is usually generated dynamically with the random principle.

  • Migrations: A crucial facet before you even write the first migration is creating a database for this app. Then add its credentials to the .env file available in the project's root.

  • Resource controller: If the project has more than one resource controller, it is advisable to create a base resource controller and then enables other resource controllers to inherit from it. It allows you to leverage the base behavior in each controller while still allowing specific actions to be overwritten for individual controllers.

At a time when single-page applications and rich internet applications (RIA) are prevalent in the market, it is imperative to utilize regular communication channels, such as RESTful APIs. Combine that with Laravel development, and you can rest assured that you have a winner on your hands!

Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

Topics:
restful api ,restful api testing ,laravel ,restful web api ,tutorial ,integration ,what is a restful api

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}