JSON is a data transmission format and has nothing to do with REST.
Unsolicited API Advice from someone who has been there
In the 2016 Laravel Survey, one of the most asked for features was "Helper classes and methods for API development". I very much agree. I've utilized no less than three open source libraries for APIs in Laravel, two of which are touted as being "for Laravel" or having a "Laravel wrapper". Bollocks. The current implementations that we have of RESTful APIs (for me JSON:API) are horrible. I had intended to contribute to one, but deleted my fork after looking too deep beneath the surface.
Protip: start new. Most of the libraries are designed to be heavily agnostic, and this is where I felt a lot of friction. They try very hard to be completely agnostic about everything, and this means you need to spend more effort on the wrapper library. The "Laravel" wrapper libraries for the major PHP JSON:API implementations are extremely shallow and fragile. Starting new is the solution I picked at work, and I'd pick it 100 times over if writing the code for Laravel.
And just so I'm not complaining, here's some actual, useful thoughts below:
- JSON:API mandates that the PK of the table not be in the attributes. I used a mixture of hidden and
getKeyName()to filter out data as a default, and left the option to define attributes explicitly for speed. - I created a custom FormRequest class to handle the issues related to validation/auth responses. You can also use this opportunity to validate incoming JSON and other housekeeping.
- I wrote one closure to handle generation of base queries from a list of models and ids. If you apply a little discrete math/logic, it is possible to have about 50 lines that can handle trivial lookups and nested resources alike. Just handle things like pagination and filters elsewhere, and you've got most of your API's lookups covered. I usually just apply a
->destroy(),->get(), etc at the end depending on the current action. - Note that you should tie directly into the route parameters if possible; I once forgot to pass the IDs in and deleted a whole table while demo-ing the code for an intern.
- I was initially trying to be data agnostic, but Eloquent does all the heavy lifting if you just let it. I don't recommend agnostic libraries, if only because Eloquent is so badass.
Please or to participate in this conversation.