Download the PHP package serhiikamolov/laravel-jsonapi without Composer
On this page you can find all versions of the php package serhiikamolov/laravel-jsonapi. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download serhiikamolov/laravel-jsonapi
More information about serhiikamolov/laravel-jsonapi
Files in serhiikamolov/laravel-jsonapi
Package laravel-jsonapi
Short Description A set of interfaces and classes to facilitate the construction of an efficient Json Api application with the Laravel framework.
License MIT
Informations about the package laravel-jsonapi
Laravel JsonAPI
A set of interfaces and classes to facilitate the construction of an efficient JSON:API application with the Laravel framework.
Table of contents
- Installation
- Custom error handler
- Request validation class
- Response class
- Serializer class
- Declaring Serializers
- Using Model Serializers
- Using Field Modifiers
- Expand Response with the Queries Log
- Testing API Responses
Installation
This package is very easy to set up via Composer, just run from your project's root folder:
Custom error handler
In order to make errors output compatible with json API format go to bootstrap/app.php
and register the custom error handler.
Or just extend default App\Exceptions\Handler
from JsonAPI\Exceptions\Handler
class.
Now, in case of an exception you will get such a response:
Request validation class
\JsonAPI\Contracts\Request
is a simple extension of the FormRequest
class that returns the validation errors in compatible with the json API form
Example of a controller with request validation:
Response with validation errors:
Response class
JsonAPI\Response\Response
is an exension of the JsonResponse
class with some additional methods.
Add additional values to the links object of the response
Return error with the specific code in the response
Return response with JWT token
Return a custom data object in the response
Attach a field to the response's data object
Add a debug information to the response object
Add a meta data to the response object
Serialize an eloquent collection or a data model
Paginate a data array in the response
Add a specific status code to the response
Response
class is an implementation of the Builder pattern thus you can use diffrent methods in a row:
Response result:
Serializer class
Serializers allow complex data such as Eloquent Collections and Model instances to be converted to a simple array that can be easily rendered into JSON. Serializer class gives you a powerful, generic way to control the output of your responses.
Declaring Serializers
Let's create a simple serializer class extended from the JsonAPI\Response\Serializer
class.
There is a public method fields()
which returns an array, and define a set of fields which will be retrieved from the given Collection or Model
and put to the response.
Here you can notice that uuid
is not taken from the database but generated from the model data.
In such a way you can define any new field you need in the response or even override an existing field value.
Using Model Serializers
There is serialize
method in the JsonAPI\Response\Response
class which accept a Serializer instance as a second parameter.
Response result
Using Field Modifiers
Field modifiers can be applied to every field defined in serializer. Although, there are a few predefined modifiers: timestamp
, number
, trim
you can define your own modifier by creating a protected
method with the modifier
prefix in its name.
Also you can use another serializing class as a modifier, it can be useful when you have some related data to the original model.
Expand Response with the Queries Log
Enable JsonAPI\Http\Middleware\JsonApiDebug
middleware and expand the debug
section of a response with the information from the queries log.
Response with the queries log.
Testing API Responses
Add JsonAPI\Traits\Tests\JsonApiAsserts
trait to your default TestCase
class and expand your tests with some useful asserts methods for testing API responses.
Example of using additional asserts:
All versions of laravel-jsonapi with dependencies
ext-json Version *
illuminate/http Version ^11.1
illuminate/database Version ^11.1
illuminate/pagination Version ^11.0
illuminate/support Version ^11.0
illuminate/routing Version ^11.0
illuminate/auth Version ^11.0