Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds


Free Preview: Build a REST API With Laravel


  • Overview
  • Transcript

REST APIs are useful for all kinds of apps. They can be the back-end to a front-end web app, they can store data for a mobile app, or they can provide services to other apps or APIs. There are a lot of moving pieces to coding a RESTful API, but having the right tools can make it a lot easier. Laravel is one such tool—a great platform for building REST APIs.

In this course, Envato Tuts+ instructor Jeremy McPeak will teach you how to write a RESTful API using Laravel. Follow along as Jeremy codes a complete API that serves data on flights, airports, and airline customers. Along the way, you'll learn how to define and seed a database, accept and validate input, and protect the API.

If you haven't used Laravel before, why not check out our full course and learn Laravel 5: Get Started With Laravel 5. If you've mastered the fundamentals of Laravel, build on your experience with these courses:

1. Introduction

1.1 Introduction

Building a restful API can be a daunting task because there are a lot of things that you need to consider. For one, you need a suitable framework that is both powerful and flexible. But it also needs to provide built in tools to make you more productive. So that you can go from development to deployment in a relatively short amount of time, and Laravel is one such framework. Hi, my name is Jeremy McPeak. And over the next hour or so, I'm going to teach you how to build a RESTful API using Laravel. We'll start from scratch by creating a brand new project and defining our data models. We'll be working with airline associated data. You know, things like flights, airports, and passengers. We'll define the relationships between our models and seed our database with some random data. Then we'll start writing our API. We'll create a controller for handling all the requests regarding our flights, starting with simple get requests. But good API's do more than just return data, they return well structured data. So we'll spend a few minutes designing our data structure, so that it makes sense to the people using our API. We'll then discuss techniques that will let users request additional data, as well as select data based upon certain criteria. From there we'll talk about post update and delete requests and how our Laravel application will handle them. So you'll learn a little bit about Laravel's resource controllers, as well as its built-in validation capabilities. And finally, and probably most importantly, we'll talk about authentication and how to protect certain parts of our API. We have a lot of ground to cover, so when you are ready, queue up the next video and we will get started.

2. Getting Started

2.1 What You Need

There are a few things that you need in order to follow along in this course. And the first is the most obvious, you need Laravel. And if you don't have Laravel on your machine or your development environment then we need to go through the process of getting Laravel. Now to get Laravel, we use a tool called Composer. So you will want to point your browser to get the composer.org and then download and install Composer. It is a very straight forward installation. And once you have that installed, you want to go to your command prompt or your terminal and you want to type composer global require "laravel/installer". And this is going to download and install Laravel. Now, this could take a minute or so, depending upon your network and machine speed. It's not going to take very long for me because I already have it installed. But while it's installing on your machine, let's talk about your development environment because Laravel has some very specific requirements. If you go to laravel.com and then click on the documentation, it's going to take you to the installation instructions. Now of course there's a lot of other information here but you're going to see the server requirements for running Laravel, you have to set up all of these things or you can use something called homestead. Homestead is a virtual machine that has all of the stuff set up. And over here on the left hand side, you can see Dev Environments. Homestead, now I highly recommend that if you don't already have your environment set up, use Homestead. But in doing so, you have to go through a few other steps. But the reason why I recommend using Homestead is for one, it has everything set up for you. It's also running inside of a virtual machine. So you don't have to worry about mocking up your physical machine with everything you need for development purposes, instead all that stuff is inside of a virtual machine. So your physical machine remains pressed in while your development environment has everything it needs. Now you can see here that Homestead is a prepackaged vagrant box. So that's the first thing that you'll need to install, vagrant is free. And it uses Oracle's Virtual Box by default. Now if you have a VMWare workstation or anything like that, you can use that as well. However the vagrant plugins for those other solutions aren't free. So you will be out some money on top of already paying for what virtual solution that you are using but virtual box is free, and of the Vagrant plugin for virtual box is free as well, so feel free to use that. Now we're not going to go through the process of installing Homestead because what you see here is actually running inside of a virtual machine. And if I send a Vagrant inside of this virtual machine, well if inception taught us anything, it's that a virtual machine running inside of another virtual machine Is extremely slow. But you can see here that Homestead gives you all of this already installed, already set up. So you don't have to do anything as far as configuration is concerned on the virtual machine. Now there are some things that you will need to do as far setting up Homestead and vagrant but you will find the installation instructions and configuration all here. Now, the third thing you will need is an HTTP debugger because we are going to be riding a restful API. We need some sort of client for issuing requests to our application. And on Windows I have always used a tool called Fiddler. Fiddler was pretty much the first tool to market, and it has always been free and hopefully it will remain to be free. And while it originally started on Windows, you can download Fiddler for other operating systems. So you will want to go to free download and then download it. But if you scroll on down you can see that there is a build for Linux and Mac. Now, you don't have to use Fiddler if you don't want to, if there is some other utility that you want to use then feel free to do so. I'm using Fiddler because it is for one, the one that I know [LAUGH] And for another it's very, very good at what it does. It allows you to issue requests and inspect both the request and the response from the server. And when you have everything installed and configured then you're ready to start designing and writing an API using Laravel. And we will get started with that in the next lesson.