2017 © Pedro Peláez
 

yii-module yiiyin

Yii 1.1 json:api module

image

dimvic/yiiyin

Yii 1.1 json:api module

  • Friday, August 11, 2017
  • by dimvic
  • Repository
  • 1 Watchers
  • 3 Stars
  • 36 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 6 % Grown

The README.md

json:api module for Yii 1.1

Packagist package ![License][ico-license], (*1)

Yii 1.1 module, drop in and configure to automagically expose resources (CActiveRecord models) through a json:api 1.0 compatible web service., (*2)

Thanks yin for being an amazing library and providing the example this module is heavily based on., (*3)

Thanks Máté Kocsis for the help and merging of the pull requests., (*4)

Supported functions

  • GET /{type}/{id}
  • GET /{type}/{id}/relationships/{relationship}
  • GET /{type}/{id}/{relationship}
  • PATCH /{type}/{id}
  • POST /{type}
  • DELETE /{type}/{id}

Usage

Simply configure the module and you have a fully functional HATEOAS web service for your models., (*5)

I like to believe configuration is self-explanatory, the only thing you should watch out for is that you need to configure a type (even with an active methods array) for each exposed relationship type:, (*6)

return [
    ...
   'modules' => [
        'yiiyin' => [
            'route' => 'api',//expose the module at /api
            'controllerMap' => [//only add this if you want all requests logged
                'default'=> [
                    'class'=>'dimvic\\YiiYin\\ApiLogController',//log using Yii::log($log, 'info', 'json:api')
                ],
            ],
            'resources' => [
                'Book' => [//exposed model
                    'type' => 'books',//exposed at api/books
                    'methods' => ['GET', 'POST', 'PATCH', 'DELETE'],//API methods supported for this model
                    'exposedRelationships' => [//all relations a client may access using the API
                        'book_i18ns' => 'book_i18ns',//relation name => API type (route)
                        'authors' => 'authors',
                        'publisher' => 'publishers',
                    ],
                    'defaultRelationships' => [//all relations a client may access using the API
                        'book_i18ns' => 'book_i18ns',//relation name => API type (route)
                        'authors' => 'authors',
                        'publisher' => 'publishers',
                    ],
                ],
                'BookI18n' => [
                    'type' => 'book_i18ns',
                    'methods' => ['GET', 'POST', 'PATCH'],
                ],
                'Author' => [
                    'type' => 'authors',
                    'methods' => ['GET', 'POST', 'PATCH'],
                ],
                'Publisher' => [
                    'type' => 'publishers',
                    'methods' => ['GET', 'POST', 'PATCH'],
                    'exposedRelationships' => ['representatives' => 'representatives'],
                    'defaultRelationships' => ['representatives' => 'representatives'],
                ],
                'Representative' => [
                    'type' => 'representatives',
                    'methods' => ['GET', 'POST', 'PATCH'],
                ],
            ],
        ],
        ...
    ],
    ...
    'components' => [
        'urlManager' => [
            'urlFormat' => 'path',
            'showScriptName' => false,
            'rules' => [
                ['class' => 'dimvic\\YiiYin\\ApiUrlRule'],
                ...
            ],
        ],
    ],
    ....
];

Demo

Example project can be found here. Setup it up in less than a minute., (*7)

TODO

  • Fix PATCH {"relationship": {"data":null}}
  • GET /{resource} paginated
  • GET ?include & eager loading for included relationships
  • GET ?filter
  • Review error codes & messages
  • Controller filter to validate requests (see yin-middlewares)
  • UUID generator for exposed models (using a behavior)
  • Allow use customized of customized repositories, transformers and hydrators

The Versions

11/08 2017