Advanced

Incremental APIs

Often, pitfalls and traps are a good thing! They teach us about what to avoid the next time around. This is how best practices emerge. With that in mind, in this series, let's take an incremental approach to understanding API development with Laravel.

Start Series

Share this series on:

  • 01

    Episode 1 Run Time 6:14

    We Need Dummy Data Free

    If we're going to build an API from scratch, naturally, we require test/dummy data to work with. Luckily, Laravel (along with the popular Faker library) makes this process a cinch.

  • 02

    Episode 2 Run Time 6:29

    Level 1: Eloquent Queries to JSON

    The "oh no she didn't" approach to building an API in Laravel is to simply return the result of an Eloquent query from your controller. Sure, Laravel is smart enough to cast this to JSON, but are you sure that's what you want to do? Let's talk about that.

  • 03

    Episode 3 Run Time 7:32

    Level 2: Responses and Codes

    Returning the proper HTTP status codes (and potentially application-specific codes) is paramount to building a successful API.

  • 04

    Episode 4 Run Time 4:29

    Level 3: Transformations

    We need to solve that issue of linking our API output to the structure of our database tables. One remedy might be to leverage transformations. We'll discuss that very thing in this lesson.

  • 05

    Episode 5 Run Time 4:24

    Extraction

    As our code currently rests, there's a violation of the single responsibility priniciple. To fix this, let's extract the transformation process into its own set of classes.

  • 06

    Episode 6 Run Time 7:26

    Refactoring

    The problem with HTTP status codes is that they can be difficult to remember. Rather than relying on "magic numbers" in our controllers, what if we instead abstract all that away to readable methods in a parent class?

  • 07

    Episode 7 Run Time 10:23

    Level 4: Basic Authentication

    What happens when we want to limit access to our API? Well, clearly, we'll need some form of authentication. Let's start with the simplest solution: HTTP basic authentication (with SSL).

  • 08

    Episode 8 Run Time 8:02

    Level 5: Seeders Reloaded

    As we prepare for a follow-up lesson on nested resources, let's return to our database seeders, and focus on refactoring and debugging.

  • 09

    Episode 9 Run Time 9:13

    Level 6: Tags and Nested Resources

    Now that we're successfully presenting lessons to the consumers of our API, let's switch over to tags. Luckily, because most of the infrastructure is in place, allowing for nested resources will be fairly painless!

  • 10

    Episode 10 Run Time 16:17

    Level 7: Tests...Readable Ones!

    Readability is the key to a successful test suite. We often throw around this word for production code, and then completely ignore the concept when it comes to testing. Don't do that! Let me help.

    View the the source for these tests on GitHub.

  • 11

    Episode 11 Run Time 13:14

    Refactoring Tests and Traits

    We made a lot of progress in the previous episode on testing our API, but there's still room for improvement. These tests need to be as simple as possible to write. How can we allow for that?

    View the source for this lesson on GitHub.

  • 12

    Episode 12 Run Time 8:19

    API Pagination

    We've ignored that pesky Lesson::all() long enough! Sure, it's fine when we have fifty records, but what about when that number becomes fifty thousand? Do you really want to fetch all those for each request? Of course you don't. Let's figure out how to paginate our API results.