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

Next lesson playing in 5 seconds

Cancel

Free Preview: Create a REST API With Lumen

Introduction

01:25
  • Overview
  • Transcript

Lumen is Laravel's little brother: a fast, lightweight micro-framework for writing RESTful APIs. With just a little bit of code, you can use Lumen to build a secure and extremely fast RESTful API.

In this course, Envato Tuts+ instructor Jeremy McPeak will show you how to get started building REST APIs with the Lumen framework. You'll start by setting up a Lumen development environment and go on to build a complete API for a music store, including routing, MySQL database connectivity, and security.

If you haven't used Lumen before, why not check out our short course: Get Started With Lumen. If you want to explore Laravel, you can build on your experience with these courses:

1. Introduction

1.1 Introduction

So you're a PHP developer and you need to write a RESTful API. You've written quite a few Laravel based applications and you know that you can whip out an API using the tools and technologies that you love. But a little voice in your head whispers doubts, questioning if Laravel is too much for your project's needs. No, you're not crazy, you're just a sensible developer wanting the best tool for the job, and you're not alone because that's exactly why we have the Lumen. Hi, my name is Jeremy McPeak, and for the next half hour or so I'm going to teach you how to write a RESTful API using Lumen, an extremely fast framework designed specifically for writing restful APIs. And one of the best parts is that it's written by Taylor Otwell, the dude behind Laravel, so you could say that Lumen is Laravel's little brother. We'll start at the very beginning, we'll download Lumen and go through the tools that you will need to follow along with me. Then you'll learn how to define routes and handle HTTP requests. We'll then write an API for a music store, giving us the ability to query and manage the guitars listed in the store's inventory. But no API is complete without authentication, and you'll learn how to protect your RESTful API using Lumen's built-in authentication middleware. So when you're ready, queue up the next video and we'll get started.

1.2 Set Up a Dev Environment

Before we do anything with Lumen, we of course need to install it, and there's really only two ways of doing that. If you go to Lumen's website, which is lumen.laravel.com, then click on the Documentation link, and you will find the installation instructions for both approaches. The first uses Laravel Homestead, which is a virtual machine. It's actually a Vagrant Box, and it has everything that you need for writing Lumen and a layer of Laravel applications. If you scroll on down here, you can see everything that is included with the Homestead Virtual Machine. Now there are some requirements that your server needs to have in order to run Lumen, at least in production, and they are listed here. Now you don't need these for development until you actually need those extensions, but, for the stuff that we are going to be doing in this course, you don't need those. So, if you plan on doing Lumen and Laravel development, pretty extensively, then definitely go the Homestead route. Otherwise, you can use Composer to install Lumen, and that's what I am going to be doing in this lesson. Now if you don't have Composer, now is a very good time to get that. Just go to getcomposer.org, download and install it for your operating system, and you will be good to go. It tells you the command that you need to run, so let me pull up my command line, and we want to run a Composer global require and then Laravel/Lumen installer. It helps if you type everything correctly the first time, but this will take a little bit to install, and, of course, it all depends upon your connection speed as well as your computer speed. After you install Lumen, you can create your first project, and you do so with Lumen > New, and then the name of your project. I'm just going to call this firstapp. We're going to use this to get acquainted with Lumen, and then we're going to throw it away and create something else. And while this is creating our project, let's look at a few other things that you'll need for this course. If you want to follow along, the first thing is MySQL Database. Now if you're using Homestead, don't worry about MySQL because Homestead has that included. Now you can go to mysequel.com and you can download and configure that, but you can also use another tool called MAMP and this is what I'm going to use. Now MAMP gives you everything you need to get started writing PHP applications. It gives you PHP, Apache, MySQL, and a few other things. Now, really, all I'm going to be using MAMP for is for MySQL because it gives you an easy way of getting MySQL, so that's really the only thing that I'm going to be using other than PHPmyadmin. The next tool you will need is an HTTP debugger. This is something that allows you to not only make requests, but it allows you to inspect them as well as the responses, and as far as Windows is concerned, the standard is Fiddler. This has been around for many, many years and it is a wonderful tool. Now there are builds for other operating systems. So, if you go to free download at telerik.com/fiddler, and the big Download button might change depending upon what platform you're on, but for me it's a Windows install. But, if you scroll on down, you can see that there are builds for Linux and Mac as well. But, if you know of an HTTP debugger for those platforms that you want to use, feel free to do so. What's important is that it allows you to make requests as well as inspect the response, okay? So, our new project is ready to go. So, let's take a look here, and let me pull up Explorer, and you'll see that there are several folders. There's a lot of these, look somewhat similar to a Laravel project, and we are going to be diving into these in later lessons. Now this public folder is our Web Server folder, if you will, it's the www. Whenever we start our development server, we want to specify this public folder, so let's go ahead and do that. I'm going to use PHP server, so PHP/s and the local host. Let's use the port 8080, although that might be taken already, so let's just do 8000, and then -t and then public, and this is going to start an HTTP server. So, we can navigate to that address, http://localhost8000, and we should see something here. We do, we have version 5.2.7 of Lumen and Laravel Components 5.2, so now we have Lumen installed. In the next lesson, we're going to poke around and get acquainted with Lumen.