PHPackages                             loadsys/cakephp\_serializers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. loadsys/cakephp\_serializers

ActiveCakephp-plugin[Templating &amp; Views](/categories/templating)

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.

1.0.0-rc.4(11y ago)63.4k1[18 issues](https://github.com/loadsys/CakePHP-Serializers/issues)[1 PRs](https://github.com/loadsys/CakePHP-Serializers/pulls)MITPHPPHP &gt;=5.4.0

Since Dec 5Pushed 11y ago9 watchersCompare

[ Source](https://github.com/loadsys/CakePHP-Serializers)[ Packagist](https://packagist.org/packages/loadsys/cakephp_serializers)[ Docs](https://github.com/loadsys/CakePHP-Serializers)[ RSS](/packages/loadsys-cakephp-serializers/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (8)Used By (0)

CakePHP-Serializers
===================

[](#cakephp-serializers)

[![Latest Version](https://camo.githubusercontent.com/3e8bdaff2b9a69eff6d4684e71f2cdd0c85f8fe252de4ac77b2952fddd433ff5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6c6f61647379732f43616b655048502d53657269616c697a6572732e7376673f7374796c653d666c61742d737175617265)](https://github.com/loadsys/CakePHP-Serializers/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/f66239e6cdf1bb0c8efa57203efde3f20ae5e28438371bde3939829e5863564d/68747470733a2f2f7472617669732d63692e6f72672f6c6f61647379732f43616b655048502d53657269616c697a6572732e7376673f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://travis-ci.org/loadsys/CakePHP-Serializers)[![Coverage Status](https://camo.githubusercontent.com/9ec06d4be9b4f89b01d97812e7b5c83440c7f36231314b0b77009447b8d92a4e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6c6f61647379732f43616b655048502d53657269616c697a6572732f62616467652e737667)](https://coveralls.io/r/loadsys/CakePHP-Serializers)[![Total Downloads](https://camo.githubusercontent.com/ae8d369d29567e31c9103eb1db811c3fa1968a6ca331d20b489ad785ebe7bfa4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6f61647379732f63616b657068705f73657269616c697a6572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/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.

This plugin is designed to match the [Ember Data](http://emberjs.com/guides/models/the-rest-adapter/)and the [DS.ActiveModelAdapter](http://emberjs.com/api/data/classes/DS.ActiveModelAdapter.html) for serialization and deserialization of CakePHP generated responses.

Questions on any implementation details can be answered typically using the Test Cases as the final authoritative answer.

This is currently not fully production ready - be warned bugs/issues may exist.

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

1. [Base Use Case](#basic-use-case)
2. [Requirements](#requirements)
3. [Installation](#installation)
4. [Basic Setup](#basic-setup)
5. [Error and Exception Handling Setup](#error-and-exception-handling-setup)
6. [Custom Bake Templates](#custom-bake-templates)
7. [Advanced Examples](#advanced-examples)
8. [Contributing](#contributing)
9. [License](#license)
10. [Copyright](#copyright)

Additional README Documents:

1. [Serialization](/Docs/SERIALIZE_README.md)
2. [Deserialization](/Docs/DESERIALIZE_README.md)
3. [Exceptions](/Docs/EXCEPTIONS_README.md)

Basic Use Case
--------------

[](#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.

So serializing a CakePHP model data array:

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

into:

```
{
	"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:

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

or:

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

into

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

Requirements
------------

[](#requirements)

- PHP &gt;= 5.4.0
- CakePHP &gt;= 2.3

Installation
------------

[](#installation)

### Composer

[](#composer)

- Run this shell command

```
$ composer require loadsys/cakephp_serializers:dev-master
```

### Git

[](#git)

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

Basic Setup
-----------

[](#basic-setup)

Load the plugin and be sure that bootstrap is set to true:

```
// 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:

```
// 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:

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

The [CakePHP book has more information on doing REST APIs](http://book.cakephp.org/2.0/en/development/rest.html)with CakePHP and this feature.

Error and Exception Handling Setup
----------------------------------

[](#error-and-exception-handling-setup)

Errors and Exceptions are handled via a separate CakePHP plugin, included via Composer: [SerializersErrors](https://github.com/loadsys/CakePHP-Serializers-Errors)

Please read the documentation there for more information on the specifics.

Modify your `app/Config/core.php` file to use the Custom Exceptions/Error handling in SerializersErrors.

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

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
---------------------

[](#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
-----------------

[](#advanced-examples)

We can serialize both multiple records:

```
$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:

```
{
	"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:

```
$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

```
{
	"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:

```
{
	"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

```
$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:

```
{
	"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

```
$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

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

into:

```
{
	"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:

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

into

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

Contributing
------------

[](#contributing)

### Reporting Issues

[](#reporting-issues)

Please use [GitHub Isuses](https://github.com/loadsys/CakePHP-Serializers/issues) for listing any known defects or issues.

### Development

[](#development)

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

The Complete Test Suite for the Plugin can be run via this command:

`./lib/Cake/Console/cake test Serializers AllSerializers`

License
-------

[](#license)

[MIT](https://github.com/loadsys/CakePHP-Serializers/blob/master/LICENSE.md)

Copyright
---------

[](#copyright)

[Loadsys Web Strategies](http://www.loadsys.com) 2015

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~30 days

Recently: every ~38 days

Total

6

Last Release

4029d ago

PHP version history (2 changes)1.0.0-beta.1PHP &gt;=5.3.0

1.0.0-rc.3PHP &gt;=5.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/58c6913a72757502a801ebc71f995eb1c4f7a53a7de7314a20b9a1f1d6131b60?d=identicon)[jtyost2](/maintainers/jtyost2)

![](https://www.gravatar.com/avatar/2380c6ad9546e6c8ce5c5b641f5a4bce0c4d3e7f3a377dc287936dff50cc3064?d=identicon)[ricog](/maintainers/ricog)

---

Top Contributors

[![justinyost](https://avatars.githubusercontent.com/u/85320?v=4)](https://github.com/justinyost "justinyost (251 commits)")[![joeytrapp](https://avatars.githubusercontent.com/u/283946?v=4)](https://github.com/joeytrapp "joeytrapp (4 commits)")[![chronon](https://avatars.githubusercontent.com/u/57735?v=4)](https://github.com/chronon "chronon (3 commits)")[![loadsysworker](https://avatars.githubusercontent.com/u/6012948?v=4)](https://github.com/loadsysworker "loadsysworker (2 commits)")[![t1mmen](https://avatars.githubusercontent.com/u/192263?v=4)](https://github.com/t1mmen "t1mmen (1 commits)")

---

Tags

jsoncakephpviewemberserializerscakephp-pluginember-data

### Embed Badge

![Health badge](/badges/loadsys-cakephp-serializers/health.svg)

```
[![Health](https://phpackages.com/badges/loadsys-cakephp-serializers/health.svg)](https://phpackages.com/packages/loadsys-cakephp-serializers)
```

###  Alternatives

[friendsofcake/cakephp-csvview

A CSV View class for CakePHP

1762.5M3](/packages/friendsofcake-cakephp-csvview)[wyrihaximus/twig-view

Twig powered View for CakePHP

804.7M1](/packages/wyrihaximus-twig-view)[entomb/slim-json-api

Slim extension to implement fast JSON API's

268156.4k4](/packages/entomb-slim-json-api)[dereuromark/cakephp-ajax

A CakePHP plugin that makes working with AJAX a piece of cake.

55255.9k1](/packages/dereuromark-cakephp-ajax)[dogancelik/slim-json

JSON middleware for Slim PHP framework

3984.0k3](/packages/dogancelik-slim-json)[dereuromark/cakephp-feed

A CakePHP plugin containing a RssView to generate RSS feeds.

1353.7k1](/packages/dereuromark-cakephp-feed)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
