Building an API / JSON based route for searching
In Chapter 6, Adding Angular to Your App we will cover Angular and in there I needed to make an API for the widget to talk to. Let's review that again here in more detail.
Getting ready
A fresh install of Laravel will do.
How to do it...
Follow the steps to build an API / JSON route for searching:
- Add our controller:
> php artisan make:controller SearchComics
- Add our route:
- Fill in the controller:
- And in the
ComicClientInterface
class, I handle thecomics
method like this. I will go into more details in the How it works… section: - Show it working at the UI level http://recipes.dev/api/v1/search:
How it works…
First, I will go over what I did above but then I will show another example to simplify it more.
As usual, I made the controller and plugged in the route. Note I use api/v1/
as a route prefix. This just helps me later on do a breaking API change at /api/v2
. There are other ways to...