Building APIs using Laravel - A simple approach to scale

A presentation at PHP South Wales Online Meetup in August 2020 in by Steve McDougall

Slide 1

Slide 1

Building APIs using Laravel A simple approach to scale

Slide 2

Slide 2

Oi, who are you? I’m Steve McDougall (JustSteveKing) ● ● ● ● PHP User Group Organiser Conference Organiser PHP Advocate Bearded man

Slide 3

Slide 3

How do people go wrong with APIs?

Slide 4

Slide 4

We have all hit that point of no return ● ● ● 100s of Models in our app directory 100s of Models in our app/Models directory More scopes than a game of Call of Duty What if there was a better way?

Slide 5

Slide 5

Setting up domains Using a simple domain approach means you can split up your business logic easily.

Slide 6

Slide 6

How do our Models look now?

Slide 7

Slide 7

What do we add to our Builder classes?

Slide 8

Slide 8

RouteServiceProvider modification

Slide 9

Slide 9

We are used to seeing this

Slide 10

Slide 10

Here is my version

Slide 11

Slide 11

JSON:API and Middleware

Slide 12

Slide 12

JSON:API has been properly registered with the IANA. Its media type designation is application/vnd.api+json.

Slide 13

Slide 13

A simple implementation

Slide 14

Slide 14

What is that about? Here is an example: application/vnd.github.v3+json We build up our content type by: type “/” “vnd.” subtype [“+” suffix] *[“;” parameter]

Slide 15

Slide 15

Handling Routing

Slide 16

Slide 16

There are quite a few ways to handle routing in Laravel

Slide 17

Slide 17

My typical approach

Slide 18

Slide 18

Let’s handle a route

Slide 19

Slide 19

A simple and flexible Action to handle a stateless request.

Slide 20

Slide 20

A clean and simple Resource

Slide 21

Slide 21

We could be a little more advanced

Slide 22

Slide 22

Handling Traits

Slide 23

Slide 23

Traits in Laravel are really handy, they allow you to share behaviour between objects to keep your code DRY. Here is one I use quite a lot on my Models:

Slide 24

Slide 24

Observing behaviour in our API

Slide 25

Slide 25

Model Observers are powerful Model Observers in Eloquent are a powerful tool when used right. Sometimes you need to share this sort of behaviour which is why in the previous example I used a Trait - however consider the following scenario: When an author creates a new post, he want this to automatically publish this post onto social media channels.

Slide 26

Slide 26

A simple approach ● ● ● Register a PostCreated event Register Listeners for each social media you wish to publish to On each listener post through the API to the social media channel. In a small scale application this would be fine. In a larger scale this is going to cause issues.

Slide 27

Slide 27

A slightly better approach ● ● ● A Model Observer handles the created event and dispatches a post to social media Job. The post to social media job handles posting to each social media API. Alternatively the Model Observer could/should dispatch a job to handle posting to each social media API separately, meaning several Jobs are dispatched. Again, this is a step forward. Posting to 3rd party APIs should be done as a background task - so we do not delay the response returning from our API.

Slide 28

Slide 28

A more advanced approach ● ● ● Our author can select per post which channels we want to share this post on, as not all social media channels are equal. Our Model Observer will dispatch a Job for each of the selected channel. When our post has been published to each channel, we can update our Post Channel relationship with meta information such as when it was posted and even a reference ID to pull analytics from the API. Not all systems will need this fine grained analytical data - but by taking a step back in our planning we can easily see how we can refactor towards it.

Slide 29

Slide 29

An Example Job

Slide 30

Slide 30

A hat tip to TDD

Slide 31

Slide 31

Laravel and API testing Laravel has a fantastic suite of testing tools, and you should be using them. The latest release of 7.* even has some great helpers for testing with a Http Client against 3rd party APIs. If you aren’t already, start adding tests - you will thank me in the long run.

Slide 32

Slide 32

A simple test

Slide 33

Slide 33

What have we covered? ● ● ● ● ● ● ● ● ● ● Cleaner Models Cleaning up Routing Loading for an API Route Declarations, and there many forms Content Type middleware and JSON:API Actioning a Request coming into our API API Resources for consistency How useful Traits can be Mode Observers and examples of when to use them Dispatchable Jobs Testing is important, even if they are simple.

Slide 34

Slide 34

APIs do not have to be hard. But they do have to be stateless.

Slide 35

Slide 35

Thanks for listening Twitter: @JustSteveKing GitHub: JustSteveKing