2017 © Pedro Peláez
 

library laravel-jsonapi

Laravel JSON-API Base.

image

pixelindustries/laravel-jsonapi

Laravel JSON-API Base.

  • Monday, May 15, 2017
  • by czim
  • Repository
  • 0 Watchers
  • 0 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

This package is abandoned!, (*1)

Please use czim/laravel-jsonapi instead., (*2)

JSON-API Base

Software License, (*3)

Basic application elements for JSON-API projects., (*4)

Offers means for quickly scaffolding JSON-API compliance for Laravel applications., (*5)

This does NOT provide the means to set up the API or the means for user authorisation., (*6)

Version Compatibility

Laravel Package
5.3.x ?
5.4.x ?

Installation

Via Composer, (*7)

``` bash $ composer require pixelindustries/laravel-jsonapi, (*8)


Add the `JsonApiServiceProvider` to your `config/app.php`: ``` php Pixelindustries\JsonApi\Providers\JsonApiServiceProvider::class,

Publish the configuration file., (*9)

``` bash php artisan vendor:publish, (*10)



### Exceptions In your `App\Exceptions\Handler`, change the `render()` method like so: ```php <?php public function render($request, Exception $exception) { if (is_jsonapi_request() || $request->wantsJson()) { return jsonapi_error($exception); } // ...

This will render exceptions thrown for all JSON-API (and JSON) requests as JSON-API error responses., (*11)

Middleware

To enforce correct headers, add the Pixelindustries\JsonApi\Http|Middleware\JsonApiHeaders middleware to the middleware group or relevant routes. You can do this by adding it to your App\Http\Kernel class:, (*12)

<?php
    protected $middlewareGroups = [
        'api' => [
            // ... 
            \Pixelindustries\JsonApi\Http\Middleware\RequireJsonApiHeader::class,
        ],
    ];

Note that this will block access to any consumers of your API that do not conform their HTTP header use to the JSON-API standard., (*13)

Documentation

Request Data

Request Query String Data

JSON-API suggests passing in filter and page data using GET parameters, such as:, (*14)

{API URL}?filter[id]=13&page[number]=2

This package offers tools for accessing this information in a standardized way:, (*15)

Using the jsonapi_query() global helper function. This returns the singleton instance of Pixelindustries\JsonApi\Support\Request\RequestParser., (*16)

<?php
    // Get the full filter data associative array.
    $filter = jsonapi_query()->getFilter();

    // Get a specific filter key value, if it is present (with a default fallback).
    $id = jsonapi_query()->getFilterValue('id', 0);

    // Get the page number.
    $page = jsonapi_query()->getPageNumber();

You can ofcourse also instantiate the request parser yourself to access these methods:, (*17)

<?php
    // Using the interface binding ...
    $jsonapi = app(\Pixelindustries\JsonApi\Contracts\Support\Request\RequestQueryParserInterface::class);

    // Or by instantiating it manually ...
    $jsonapi = new \Pixelindustries\JsonApi\Support\Request\RequestQueryParser(request());

    // After this, the same methods are available
    $id = $jsonapi->getFilterValue('id');

Request Body Data

For PUT and POST requests with JSON-API formatted body content, a special FormRequest is provided to validate and access request body data (\Pixelindustries\JsonApi\Http\Requests\JsonApiRequest)., (*18)

This class may be extended and used as any FormRequest class in Laravel., (*19)

There is also a global help function jsonapi_request(), that returns an instance of this class (and thus mimics Laravel's request())., (*20)

<?php
    // Get validated data for the current request
    $jsonApiType = jsonapi_request()->getType();
    $jsonApiId   = jsonapi_request()->getId();

Encoding

This package offers an encoder to generate valid JSON-API output for variable input content., (*21)

With some minor setup, it is possible to generate JSON output according to JSON-API specs for Eloquent models and errors., (*22)

Eloquent models, single, collected or paginated, will be serialized as JSON-API resources., (*23)

More information on encoding and configuring resources., (*24)

Custom Encoding & Transformation

To use your own transformers for specific class FQNs for the content to be encoded, map them in the jsonapi.transform.map configuration key:, (*25)

<?php
    'map' => [
        \Your\ContentClassFqn\Here::class => \Your\TransformerClassFqn\Here::class,        
    ],

This mapping will return the first-matched for content using is_a() checks. More specific matches should be higher in the list., (*26)

As a last resort, you can always extend and/or rebind the Pixelindustries\JsonApi\Encoder\Factories\TransformerFactory to provide your own transformers based on given content type., (*27)

Contributing

Please see CONTRIBUTING for details., (*28)

Credits

License

The MIT License (MIT). Please see License File for more information., (*29)

The Versions