SzymonDukla / slim3-jsonapi by SzymonDukla
forked from carterzenk/slim3-jsonapi

Slim 3 Eloquent JSON API Transformer Package
18
0
1
Package Data
Maintainer Username: SzymonDukla
Maintainer Contact: hello@szymondukla.com (Szymon Dukla)
Package Create Date: 2019-02-26
Package Last Update: 2019-02-27
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2021-04-06 15:26:39
Package Statistics
Total Downloads: 18
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 1
Total Open Issues: 0

Slim 3 Eloquent JSON API Server Package

Compatible with Slim Framework 3.0, 3.1

  • A JSON API Transformer that will allow you to convert any mapped object into a valid JSON API resource.
  • Controller boilerplate to write a fully compiliant JSON API Server using your exisiting Eloquent Models.
  • Works with Slim Framework 3 and Eloquent 5.

Installation

Use Composer to install the package:

composer require szymondukla/slim3-json-transformer

Configuration for Slim 3 and Eloquent

Step 1: Define Mappings

Open up your settings file and add an entry for your mappings:

return [
    'settings' => [

        // JSON-API settings
        'json-api' => [
            'mappings' => [
                Mappings\ContactMapping::class,
                Mappings\OwnerMapping::class
            ]
        ]

    ]
];

Step 2: Define Dependency

Open up your dependencies file and add an entry for the JsonApiSerializer, using the mappings you defined in the settings.

$container[SzymonDukla\Slim3\JsonApi\JsonApiSerializer::class] = function (ContainerInterface $c) {
    $mappings = $c->get('settings')['json-api']['mappings'];

    $mapper = new SzymonDukla\Slim3\JsonApi\Mapper\Mapper($mappings);

    $mappingHelper = new SzymonDukla\Slim3\JsonApi\Mapper\MappingHelper();
    $parsedRoutes = $mappingHelper->parseRoutes($mapper);

    $transformer = new AmaranthCloud\Api\JsonApi\JsonApiTransformer($parsedRoutes);

    return new SzymonDukla\Slim3\JsonApi\JsonApiSerializer($transformer);
};