Laravel API 101:
We will create a new Laravel application which connects to the API that we have built in the previous lessons.
- Quick Recap
- Getting started
- The service class
It's alive! In this lesson, we will create a new Laravel 5.3 application which will use Guzzle to connect to our API that we have built in the previous lessons. We will fetch the articles, display them on the homepage and provide links to view each article.
We will create a service class which will handle retrieving the articles from the API. In return, we will get a Collection with which we can do whatever we want. We will be using the
show methods from our API to retrieve articles.
Why this approach?
If you have a single source of truth, in this case, the articles database, and you want to display them on multiple sites, it is much easier to retrieve them from the API than to duplicate models on other sites and connect directly to the database. If you duplicate models, you will have to maintain them, and trust me this quickly becomes a pain in the ass.
You can change the technology and code behind the API, but as long as it returns the same formatted responses, all other sites that depend on it will continue to work.
This way it is much easier to maintain applications since we have decoupled the technologies behind them.
- Cover image Winter scene created by Marius
To continue reading, choose a subscription plan: