2017 © Pedro Peláez
 

cakephp-plugin cakephp_serializers

A solution to serialize CakePHP response to JSON and correspondingly deserialize JSON into CakePHP data arrays, designed primarily around building REST APIs with Ember and Ember Data.

image

loadsys/cakephp_serializers

A solution to serialize CakePHP response to JSON and correspondingly deserialize JSON into CakePHP data arrays, designed primarily around building REST APIs with Ember and Ember Data.

  • Friday, May 8, 2015
  • by jtyost2
  • Repository
  • 9 Watchers
  • 6 Stars
  • 3,281 Installations
  • PHP
  • 0 Dependents
  • 1 Suggesters
  • 2 Forks
  • 19 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

CakePHP-Serializers

Latest Version Software License Build Status Coverage Status Total Downloads, (*1)

A solution to serialize CakePHP response to JSON and correspondingly deserialize JSON into CakePHP data arrays, designed primarily around building REST APIs with Ember and Ember Data., (*2)

This plugin is designed to match the Ember Data and the DS.ActiveModelAdapter for serialization and deserialization of CakePHP generated responses., (*3)

Questions on any implementation details can be answered typically using the Test Cases as the final authoritative answer., (*4)

This is currently not fully production ready - be warned bugs/issues may exist., (*5)

This README is split into the following sections, with additional README documents covering certain topics., (*6)

  1. Base Use Case
  2. Requirements
  3. Installation
  4. Basic Setup
  5. Error and Exception Handling Setup
  6. Custom Bake Templates
  7. Advanced Examples
  8. Contributing
  9. License
  10. Copyright

Additional README Documents:, (*7)

  1. Serialization
  2. Deserialization
  3. Exceptions

Basic Use Case

The basic concept for this plugin is to create an end to end solution for serializing and deserializing CakePHP responses into JSON. This plugin is primarily designed around the use of Ember and Ember Data with the ActiveModelAdapter., (*8)

So serializing a CakePHP model data array:, (*9)

$data = array(
    'User' => array(
        'id' => 1,
        'username' => 'testusername',
        'first_name' => 'first',
        'last_name' => 'last',
        'is_active' => true,
    )
);

into:, (*10)

{
    "user": {
        "id": 1,
        "username": "testusername",
        "first_name": "first",
        "last_name": "last",
        "is_active": true,
    }
}

And to perform the reverse, by deserializing data passed in the request body:, (*11)

{
    "users": {
        "id": 1,
        "username": "testusername",
        "first_name": "first",
        "last_name": "last",
        "is_active": true,
    }
}

or:, (*12)

{
    "user": {
        "id": 1,
        "username": "testusername",
        "first_name": "first",
        "last_name": "last",
        "is_active": true,
    }
}

into, (*13)

$this->request->data = array(
    'User' => array(
        'id' => 1,
        'username' => 'testusername',
        'first_name' => 'first',
        'last_name' => 'last',
        'is_active' => true,
    )
);

Requirements

  • PHP >= 5.4.0
  • CakePHP >= 2.3

Installation

Composer

  • Run this shell command
$ composer require loadsys/cakephp_serializers:dev-master

Git

git clone git@github.com:loadsys/CakePHP-Serializers.git Plugin/Serializers

Basic Setup

Load the plugin and be sure that bootstrap is set to true:, (*14)

// Config/boostrap.php
CakePlugin::load('Serializers', array('bootstrap' => true));
// or
CakePlugin::loadAll(array(
    'Serializers' => array('bootstrap' => true),
));

If you are planning on using this plugin, to deserialize data in an HTTP request a few other changes are required:, (*15)

// Config/boostrap.php
Configure::write('Dispatcher.filters', array(
    'Serializers.DeserializerFilter',
));

When deserializing data and setting your CakePHP controller to respond to REST HTTP requests you will also need to add:, (*16)

// Config/routes.php
Router::mapResources(array(
    '{controller_name}',
));
Router::parseExtensions('json');

The CakePHP book has more information on doing REST APIs with CakePHP and this feature., (*17)

Error and Exception Handling Setup

Errors and Exceptions are handled via a separate CakePHP plugin, included via Composer: SerializersErrors, (*18)

Please read the documentation there for more information on the specifics., (*19)

Modify your app/Config/core.php file to use the Custom Exceptions/Error handling in SerializersErrors., (*20)

``` php Configure::write('Exception', array( 'handler' => 'ErrorHandler::handleException', 'renderer' => 'SerializersErrors.SerializerExceptionRenderer', 'log' => true, ));, (*21)


This does two things: * Errors and Exceptions get output as correctly formatted JSON API, JSON or HTML depending on the request type * Allows the use of Custom Exceptions that match Ember Data exceptions for error cases ## Custom Bake Templates ## There are custom bake templates included in this project for baking your CakePHP Controller classes. Use the `serializers` template when baking a Controller, to generate a Controller to work with the Serializers Plugin. The Custom Bake Templates include using the Custom Exception Classes from the above section to provide feedback that matches Ember Data's expectations. ## Advanced Examples ## We can serialize both multiple records: ```php $data = array( 'User' => array( 0 => array( 'id' => 1, 'username' => 'testusername', 'first_name' => 'first', 'last_name' => 'last', 'is_active' => true, ), 1 => array( 'id' => 2, 'username' => 'testusername', 'first_name' => 'first', 'last_name' => 'last', 'is_active' => true, ), ) );

into:, (*22)

{
    "users": [
        {
            "id": 1,
            "username": "testusername",
            "first_name": "first",
            "last_name": "last",
            "is_active": true,
        },
        {
            "id": 2,
            "username": "testusername",
            "first_name": "first",
            "last_name": "last",
            "is_active": true,
        },
    ]
}

And serialize sub model records, even if there are multiple records:, (*23)

$data = array(
    'User' => array(
        0 => array(
            'id' => 1,
            'username' => 'testusername',
            'first_name' => 'first',
            'last_name' => 'last',
            'is_active' => true,
            'SecondaryModel' => array(
                "something" => "blahh",
            ),
        ),
        1 => array(
            'id' => 2,
            'username' => 'testusername',
            'first_name' => 'first',
            'last_name' => 'last',
            'is_active' => true,
            'SecondaryModel' => array(
                0 => array(
                    "something" => "teasdf",
                ),
                1 => array(
                    "something" => "fgdfghdfg",
                ),
            ),
        ),
    )
);

into, (*24)

{
    "users": [
        {
            "id": 1,
            "username": "testusername",
            "first_name": "first",
            "last_name": "last",
            "is_active": true,
            "secondary_models": [
                {
                    "something": "blahh"
                }
            ]
        },
        {
            "id": 2,
            "username": "testusername",
            "first_name": "first",
            "last_name": "last",
            "is_active": true,
            "secondary_models": [
                {
                    "something": "teasdf"
                },
                {
                    "something": "fgdfghdfg"
                }
            ]
        },
    ]
}

The same with deserialize both multiple records:, (*25)

{
    "users": [
        {
            "id" : 1,
            "username": "testusername",
            "first_name": "first",
            "last_name": "last",
            "is_active": true,
        },
        {
            "id": 2,
            "username": "testusername",
            "first_name": "first",
            "last_name": "last",
            "is_active": true,
        },
    ]
}

into, (*26)

$this->request->data = array(
    'User' => array(
        0 => array(
            'id' => 1,
            'username' => 'testusername',
            'first_name' => 'first',
            'last_name' => 'last',
            'is_active' => true,
        ),
        1 => array(
            'id' => 2,
            'username' => 'testusername',
            'first_name' => 'first',
            'last_name' => 'last',
            'is_active' => true,
        ),
    )
);

And deserialize sub model records, even if there are multiple records:, (*27)

{
    "users": [
        {
            "id": 1,
            "username": "testusername",
            "first_name": "first",
            "last_name": "last",
            "is_active": true,
            "secondary_models": {
                "something": "blahh",
            }
        },
        {
            "id": 2,
            "username": "testusername",
            "first_name": "first",
            "last_name": "last",
            "is_active": true,
            "secondary_models": [
                {
                    "something": "teasdf",
                },
                {
                    "something": "fgdfghdfg",
                }
            ]
        },
    ]
}

into, (*28)

$this->request->data = array(
    'User' => array(
        0 => array(
            'id' => 1,
            'username' => 'testusername',
            'first_name' => 'first',
            'last_name' => 'last',
            'is_active' => true,
            'SecondaryModel' => array(
                "something" => "blahh",
            ),
        ),
        1 => array(
            'id' => 2,
            'username' => 'testusername',
            'first_name' => 'first',
            'last_name' => 'last',
            'is_active' => true,
            'SecondaryModel' => array(
                0 => array(
                    "something" => "teasdf",
                ),
                1 => array(
                    "something" => "fgdfghdfg",
                ),
            ),
        ),
    )
);

If there is a second top level model in the data to be serialized it is moved to a property of the first model, (*29)

$data = array(
    'User' => array(
        'id' => 1,
        'username' => 'testusername',
        'first_name' => 'first',
        'last_name' => 'last',
        'is_active' => true,
    ),
    'SecondModel' => array(
        'id' => 1,
        'name' => 'asdflkjasdf'
    ),
);

into:, (*30)

{
    "user": {
        "id": 1,
        "username": "testusername",
        "first_name": "first",
        "last_name": "last",
        "is_active": true,
        "second_models": [
            {
                'id': 1,
                'name': 'asdflkjasdf'
            }
        ]
    }
}

If there is a second top level model in the data to be deserialized, it is ignored:, (*31)

{
    "users": {
        "id": 1,
        "username": "testusername",
        "first_name": "first",
        "last_name": "last",
        "is_active": true,
    },
    "second_models": {
        "id": 1,
        "something": "data"
    }
}

into, (*32)

$this->request->data = array(
    'User' => array(
        'id' => 1,
        'username' => 'testusername',
        'first_name' => 'first',
        'last_name' => 'last',
        'is_active' => true,
    )
);

Contributing

Reporting Issues

Please use GitHub Isuses for listing any known defects or issues., (*33)

Development

When developing this plugin, please fork and issue a PR for any new development., (*34)

The Complete Test Suite for the Plugin can be run via this command:, (*35)

./lib/Cake/Console/cake test Serializers AllSerializers, (*36)

License

MIT, (*37)

Loadsys Web Strategies 2015, (*38)

The Versions

08/05 2015

dev-f/request-based-serialization

dev-f/request-based-serialization https://github.com/loadsys/CakePHP-Serializers

A solution to serialize CakePHP response to JSON and correspondingly deserialize JSON into CakePHP data arrays, designed primarily around building REST APIs with Ember and Ember Data.

  Sources   Download

MIT

The Requires

 

json cakephp view ember ember-data serializers cakephp-plugin

08/05 2015

dev-master

9999999-dev https://github.com/loadsys/CakePHP-Serializers

A solution to serialize CakePHP response to JSON and correspondingly deserialize JSON into CakePHP data arrays, designed primarily around building REST APIs with Ember and Ember Data.

  Sources   Download

MIT

The Requires

 

json cakephp view ember ember-data serializers cakephp-plugin

07/05 2015

1.0.0-rc.4

1.0.0.0-RC4 https://github.com/loadsys/CakePHP-Serializers

A solution to serialize CakePHP response to JSON and correspondingly deserialize JSON into CakePHP data arrays, designed primarily around building REST APIs with Ember and Ember Data.

  Sources   Download

MIT

The Requires

 

json cakephp view ember ember-data serializers cakephp-plugin

26/03 2015

1.0.0-rc.3

1.0.0.0-RC3 https://github.com/loadsys/CakePHP-Serializers

A solution to serialize CakePHP response to JSON and correspondingly deserialize JSON into CakePHP data arrays, designed primarily around building REST APIs with Ember and Ember Data.

  Sources   Download

MIT

The Requires

 

json cakephp view ember ember-data serializers cakephp-plugin

09/02 2015

1.0.0-rc.2

1.0.0.0-RC2 https://github.com/loadsys/CakePHP-Serializers

An object oriented solution to CakePHP model data serialization to JSON.

  Sources   Download

MIT

The Requires

 

json cakephp view serializers cakephp-plugin

08/01 2015

1.0.0-rc.1

1.0.0.0-RC1 https://github.com/loadsys/CakePHP-Serializers

An object oriented solution to CakePHP model data serialization to JSON.

  Sources   Download

MIT

The Requires

 

json cakephp view serializers cakephp-plugin

05/12 2014

1.0.0-beta.1

1.0.0.0-beta1 https://github.com/loadsys/CakePHP-Serializers

An object oriented solution to CakePHP model data serialization to JSON.

  Sources   Download

MIT

The Requires

 

json cakephp view serializers cakephp-plugin

05/12 2014

1.0.0-beta.2

1.0.0.0-beta2 https://github.com/loadsys/CakePHP-Serializers

An object oriented solution to CakePHP model data serialization to JSON.

  Sources   Download

MIT

The Requires

 

json cakephp view serializers cakephp-plugin