Writing APIs with Lumen


This book is no longer available for sale.

Writing APIs with Lumen

A Hands-on Guide to Writing API Services With PHP

About the Book

Learn to Write REST APIs from Scratch

Want to learn how to write fully-tested REST APIs and microservices with PHP? I will walk you through developing an API and teach you how to test your code along the way.

In this book you will learn how to use Lumen—a micro-framework by Laravel—to write bullet-proof APIs. Lumen helps you write productive, maintainable APIs using modern application design.

This book is suitable for PHP developers with no Laravel experience. Only a basic understanding of HTTP and writing PHP applications is needed to get started.

What's Inside the Book?

You will learn how to write fully-tested APIs and understand essential Lumen concepts used to build a solid foundation for writing API projects. You will also learn:

  • To Maintain your API's database structure through built-in database migrations
  • Writing Tests with factory data in a test database
  • Responding with Consistent data output in JSON
  • How to respond to PHP Exceptions with JSON Responses
  • How to Create, Read, Update, and Delete REST Resources
  • How to represent model associations in API responses
  • A solid foundation for writing tests with PHPUnit and Mockery
  • Data Validation

This book is not only teaching you Lumen, it will walk you through how to use TDD to write applications and mock objects. This book is basically two books in one, in an easy-to-follow tutorial format.

About the Author

Paul Redmond
Paul Redmond

Hello! I'm Paul Redmond. I'm a full stack developer writing stuff with Laravel, Vue.js, and Docker. I am passionate about being productive, writing clean code, and creating simple infrastructure.

I am the author of Writing APIs with Lumen, now published with Apress as Lumen Programming Guide. You can also find out more about my latest course, Docker for PHP Developers at https://bitpress.io/docker-for-php-developers/

Reader Testimonials

Daniel Baron
Daniel Baron

Freelance Web Developer

If you need a head-start on how to use Lumen for stateless APIs, you should get this book. It shows the concepts on how to design, develop and test an API from the very beginning.

Michael Dyrynda
Michael Dyrynda

Developer, Hostworks

Paul’s “Writing APIs with Lumen” is an excellent resource for anybody looking to build APIs with the Lumen Framework. It is a thorough introduction not only to Lumen, but to writing code through test driven development using the tools available to the framework.

Table of Contents

  • Introduction
    • The Same Tools to Write APIs and Web Applications
    • Who This Book is For
    • Conventions Used in This Book
    • Tools You Will Need
    • About Me
  • Chapter 1: Installing Lumen
    • Homestead
    • Mac OSX
    • Linux
    • Windows
    • Conclusion
  • Chapter 2: Hello Lumen
    • 2.1: Setting Up a New Project
    • 2.2: Routes
    • 2.3: Middleware and Responses
    • 2.4: Views
    • Onward
  • Chapter 3: Creating the Book Application
    • Build Something Amazing
    • Environment Setup
    • Checking Unit Tests
    • Setup Complete
  • Chapter 4: Starting the Books API
    • Creating the First Endpoint
    • Setting up Models and Seed Data
    • Eloquent Books
    • Success
  • Chapter 5: Creating, Reading, Updating, and Deleting Books
    • 5.1: Requesting an Individual Book
    • 5.2: Creating a New Book
    • 5.3: Updating an Existing Book
    • 5.4: Deleting books
    • Conclusion
  • Chapter 6: Responding to Errors
    • 6.1: Test Database
    • 6.2: Better Error Responses
    • 6.3: Testing the Exception Handler
    • Conclusion
  • Chapter 7: Leveling Up Responses
    • 7.1: Introducing Fractal
    • 7.2: First Version of API Response Formatting
    • 7.2: The FractalResponse Class
    • 7.3: FractalResponse Service
    • 7.4: Integrating the FractalResponse Service
    • Conclusion
  • Chapter 8: Validation
    • 8.1: First Attempt at Validation
    • 8.2: More Validation Constraints
    • 8.3: Custom Validation Messages
    • Conclusion
  • Chapter 9: Authors
    • 9.1: The Authors database schema
    • 9.2: Fixing Broken Tests
    • Conclusion
  • Chapter 10: The /authors API Resource
    • 10.1: The GET /authors Endpoint
    • 10.2: The GET /authors/{id} Endpoint
    • 10.3: The POST /authors Endpoint
    • 10.4: The PUT /authors/{id} Endpoint
    • 10.5: The DELETE /authors/{id} Endpoint
    • Conclusion
  • Chapter 11: Book Bundles
    • 11.1: Defining the Relationship Between Books and Bundles
    • 11.2: The GET /bundles/{id} Endpoint
    • 11.3: Adding a Book to a Bundle
    • 11.4: Remove a Book from a Bundle
    • Conclusion
  • Chapter 12: Ratings
    • 12.1: Database Design
    • 12.2: Rating an Author
    • 12.3: Ratings in the Author API
    • 12.4: Eager Loading Ratings
    • Conclusion
  • Where To Go From Here
    • Laravel
    • Laracasts
    • Mockery
    • Guzzle

The Leanpub 45-day 100% Happiness Guarantee

Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms

Do Well. Do Good.

Authors have earned$10,462,200writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF, EPUB and/or MOBI files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub