{"id":19571760,"url":"https://github.com/loadsys/cakephp-serializers","last_synced_at":"2025-04-27T03:32:38.182Z","repository":{"id":9322910,"uuid":"11166981","full_name":"loadsys/CakePHP-Serializers","owner":"loadsys","description":"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.","archived":false,"fork":false,"pushed_at":"2015-05-08T22:00:18.000Z","size":2049,"stargazers_count":6,"open_issues_count":19,"forks_count":1,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-04-25T22:43:02.782Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/loadsys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-07-04T04:19:38.000Z","updated_at":"2019-08-13T15:23:09.000Z","dependencies_parsed_at":"2022-09-07T01:53:42.129Z","dependency_job_id":null,"html_url":"https://github.com/loadsys/CakePHP-Serializers","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loadsys%2FCakePHP-Serializers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loadsys%2FCakePHP-Serializers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loadsys%2FCakePHP-Serializers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loadsys%2FCakePHP-Serializers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/loadsys","download_url":"https://codeload.github.com/loadsys/CakePHP-Serializers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251085145,"owners_count":21533821,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T06:19:56.790Z","updated_at":"2025-04-27T03:32:37.659Z","avatar_url":"https://github.com/loadsys.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CakePHP-Serializers #\n\n[![Latest Version](https://img.shields.io/github/release/loadsys/CakePHP-Serializers.svg?style=flat-square)](https://github.com/loadsys/CakePHP-Serializers/releases)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Build Status](https://travis-ci.org/loadsys/CakePHP-Serializers.svg?branch=master\u0026style=flat-square)](https://travis-ci.org/loadsys/CakePHP-Serializers)\n[![Coverage Status](https://coveralls.io/repos/loadsys/CakePHP-Serializers/badge.svg)](https://coveralls.io/r/loadsys/CakePHP-Serializers)\n[![Total Downloads](https://img.shields.io/packagist/dt/loadsys/cakephp_serializers.svg?style=flat-square)](https://packagist.org/packages/loadsys/cakephp_serializers)\n\nA 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.\n\nThis plugin is designed to match the [Ember Data](http://emberjs.com/guides/models/the-rest-adapter/)\nand the [DS.ActiveModelAdapter](http://emberjs.com/api/data/classes/DS.ActiveModelAdapter.html) for\nserialization and deserialization of CakePHP generated responses.\n\nQuestions on any implementation details can be answered typically using the Test\nCases as the final authoritative answer.\n\nThis is currently not fully production ready - be warned bugs/issues may exist.\n\nThis README is split into the following sections, with additional README documents\ncovering certain topics.\n\n1. [Base Use Case](#basic-use-case)\n1. [Requirements](#requirements)\n1. [Installation](#installation)\n1. [Basic Setup](#basic-setup)\n1. [Error and Exception Handling Setup](#error-and-exception-handling-setup)\n1. [Custom Bake Templates](#custom-bake-templates)\n1. [Advanced Examples](#advanced-examples)\n1. [Contributing](#contributing)\n1. [License](#license)\n1. [Copyright](#copyright)\n\nAdditional README Documents:\n\n1. [Serialization](/Docs/SERIALIZE_README.md)\n1. [Deserialization](/Docs/DESERIALIZE_README.md)\n1. [Exceptions](/Docs/EXCEPTIONS_README.md)\n\n## Basic Use Case ##\n\nThe basic concept for this plugin is to create an end to end solution for serializing\nand deserializing CakePHP responses into JSON. This plugin is primarily designed around\nthe use of Ember and Ember Data with the ActiveModelAdapter.\n\nSo serializing a CakePHP model data array:\n\n```php\n$data = array(\n\t'User' =\u003e array(\n\t\t'id' =\u003e 1,\n\t\t'username' =\u003e 'testusername',\n\t\t'first_name' =\u003e 'first',\n\t\t'last_name' =\u003e 'last',\n\t\t'is_active' =\u003e true,\n\t)\n);\n```\n\ninto:\n\n```javascript\n{\n\t\"user\": {\n\t\t\"id\": 1,\n\t\t\"username\": \"testusername\",\n\t\t\"first_name\": \"first\",\n\t\t\"last_name\": \"last\",\n\t\t\"is_active\": true,\n\t}\n}\n```\n\nAnd to perform the reverse, by deserializing data passed in the request body:\n\n```javascript\n{\n\t\"users\": {\n\t\t\"id\": 1,\n\t\t\"username\": \"testusername\",\n\t\t\"first_name\": \"first\",\n\t\t\"last_name\": \"last\",\n\t\t\"is_active\": true,\n\t}\n}\n```\n\nor:\n\n```javascript\n{\n\t\"user\": {\n\t\t\"id\": 1,\n\t\t\"username\": \"testusername\",\n\t\t\"first_name\": \"first\",\n\t\t\"last_name\": \"last\",\n\t\t\"is_active\": true,\n\t}\n}\n```\n\ninto\n\n```php\n$this-\u003erequest-\u003edata = array(\n\t'User' =\u003e array(\n\t\t'id' =\u003e 1,\n\t\t'username' =\u003e 'testusername',\n\t\t'first_name' =\u003e 'first',\n\t\t'last_name' =\u003e 'last',\n\t\t'is_active' =\u003e true,\n\t)\n);\n```\n\n## Requirements ##\n\n* PHP \u003e= 5.4.0\n* CakePHP \u003e= 2.3\n\n## Installation ##\n\n### Composer ###\n\n* Run this shell command\n\n```bash\n$ composer require loadsys/cakephp_serializers:dev-master\n```\n\n### Git ###\n\n```bash\ngit clone git@github.com:loadsys/CakePHP-Serializers.git Plugin/Serializers\n```\n\n## Basic Setup ##\n\nLoad the plugin and be sure that bootstrap is set to true:\n\n```php\n// Config/boostrap.php\nCakePlugin::load('Serializers', array('bootstrap' =\u003e true));\n// or\nCakePlugin::loadAll(array(\n\t'Serializers' =\u003e array('bootstrap' =\u003e true),\n));\n```\n\nIf you are planning on using this plugin, to deserialize data in an HTTP request \na few other changes are required:\n\n```php\n// Config/boostrap.php\nConfigure::write('Dispatcher.filters', array(\n\t'Serializers.DeserializerFilter',\n));\n```\n\nWhen deserializing data and setting your CakePHP controller to respond to REST\nHTTP requests you will also need to add:\n\n```php\n// Config/routes.php\nRouter::mapResources(array(\n\t'{controller_name}',\n));\nRouter::parseExtensions('json');\n```\n\nThe [CakePHP book has more information on doing REST APIs](http://book.cakephp.org/2.0/en/development/rest.html)\nwith CakePHP and this feature.\n\n## Error and Exception Handling Setup ##\n\nErrors and Exceptions are handled via a separate CakePHP plugin, \nincluded via Composer: [SerializersErrors](https://github.com/loadsys/CakePHP-Serializers-Errors)\n\nPlease read the documentation there for more information on the specifics.\n\nModify your `app/Config/core.php` file to use the Custom Exceptions/Error\nhandling in SerializersErrors.\n\n``` php\nConfigure::write('Exception', array(\n\t'handler' =\u003e 'ErrorHandler::handleException',\n\t'renderer' =\u003e 'SerializersErrors.SerializerExceptionRenderer',\n\t'log' =\u003e true,\n));\n```\n\nThis does two things:\n\n* Errors and Exceptions get output as correctly formatted JSON API, JSON or HTML \ndepending on the request type\n* Allows the use of Custom Exceptions that match Ember Data exceptions for error cases\n\n## Custom Bake Templates ##\n\nThere are custom bake templates included in this project for baking your CakePHP \nController classes. Use the `serializers` template when baking a Controller, to \ngenerate a Controller to work with the Serializers Plugin.\n\nThe Custom Bake Templates include using the Custom Exception Classes from the above\nsection to provide feedback that matches Ember Data's expectations.\n\n## Advanced Examples ##\n\nWe can serialize both multiple records:\n\n```php\n$data = array(\n\t'User' =\u003e array(\n\t\t0 =\u003e array(\n\t\t\t'id' =\u003e 1,\n\t\t\t'username' =\u003e 'testusername',\n\t\t\t'first_name' =\u003e 'first',\n\t\t\t'last_name' =\u003e 'last',\n\t\t\t'is_active' =\u003e true,\n\t\t),\n\t\t1 =\u003e array(\n\t\t\t'id' =\u003e 2,\n\t\t\t'username' =\u003e 'testusername',\n\t\t\t'first_name' =\u003e 'first',\n\t\t\t'last_name' =\u003e 'last',\n\t\t\t'is_active' =\u003e true,\n\t\t),\n\t)\n);\n```\n\ninto:\n\n```javascript\n{\n\t\"users\": [\n\t\t{\n\t\t\t\"id\": 1,\n\t\t\t\"username\": \"testusername\",\n\t\t\t\"first_name\": \"first\",\n\t\t\t\"last_name\": \"last\",\n\t\t\t\"is_active\": true,\n\t\t},\n\t\t{\n\t\t\t\"id\": 2,\n\t\t\t\"username\": \"testusername\",\n\t\t\t\"first_name\": \"first\",\n\t\t\t\"last_name\": \"last\",\n\t\t\t\"is_active\": true,\n\t\t},\n\t]\n}\n```\n\nAnd serialize sub model records, even if there are multiple records:\n\n```php\n$data = array(\n\t'User' =\u003e array(\n\t\t0 =\u003e array(\n\t\t\t'id' =\u003e 1,\n\t\t\t'username' =\u003e 'testusername',\n\t\t\t'first_name' =\u003e 'first',\n\t\t\t'last_name' =\u003e 'last',\n\t\t\t'is_active' =\u003e true,\n\t\t\t'SecondaryModel' =\u003e array(\n\t\t\t\t\"something\" =\u003e \"blahh\",\n\t\t\t),\n\t\t),\n\t\t1 =\u003e array(\n\t\t\t'id' =\u003e 2,\n\t\t\t'username' =\u003e 'testusername',\n\t\t\t'first_name' =\u003e 'first',\n\t\t\t'last_name' =\u003e 'last',\n\t\t\t'is_active' =\u003e true,\n\t\t\t'SecondaryModel' =\u003e array(\n\t\t\t\t0 =\u003e array(\n\t\t\t\t\t\"something\" =\u003e \"teasdf\",\n\t\t\t\t),\n\t\t\t\t1 =\u003e array(\n\t\t\t\t\t\"something\" =\u003e \"fgdfghdfg\",\n\t\t\t\t),\n\t\t\t),\n\t\t),\n\t)\n);\n```\n\ninto\n\n```javascript\n{\n\t\"users\": [\n\t\t{\n\t\t\t\"id\": 1,\n\t\t\t\"username\": \"testusername\",\n\t\t\t\"first_name\": \"first\",\n\t\t\t\"last_name\": \"last\",\n\t\t\t\"is_active\": true,\n\t\t\t\"secondary_models\": [\n\t\t\t\t{\n\t\t\t\t\t\"something\": \"blahh\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"id\": 2,\n\t\t\t\"username\": \"testusername\",\n\t\t\t\"first_name\": \"first\",\n\t\t\t\"last_name\": \"last\",\n\t\t\t\"is_active\": true,\n\t\t\t\"secondary_models\": [\n\t\t\t\t{\n\t\t\t\t\t\"something\": \"teasdf\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"something\": \"fgdfghdfg\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t]\n}\n```\n\nThe same with deserialize both multiple records:\n\n```javascript\n{\n\t\"users\": [\n\t\t{\n\t\t\t\"id\" : 1,\n\t\t\t\"username\": \"testusername\",\n\t\t\t\"first_name\": \"first\",\n\t\t\t\"last_name\": \"last\",\n\t\t\t\"is_active\": true,\n\t\t},\n\t\t{\n\t\t\t\"id\": 2,\n\t\t\t\"username\": \"testusername\",\n\t\t\t\"first_name\": \"first\",\n\t\t\t\"last_name\": \"last\",\n\t\t\t\"is_active\": true,\n\t\t},\n\t]\n}\n```\n\ninto\n\n```php\n$this-\u003erequest-\u003edata = array(\n\t'User' =\u003e array(\n\t\t0 =\u003e array(\n\t\t\t'id' =\u003e 1,\n\t\t\t'username' =\u003e 'testusername',\n\t\t\t'first_name' =\u003e 'first',\n\t\t\t'last_name' =\u003e 'last',\n\t\t\t'is_active' =\u003e true,\n\t\t),\n\t\t1 =\u003e array(\n\t\t\t'id' =\u003e 2,\n\t\t\t'username' =\u003e 'testusername',\n\t\t\t'first_name' =\u003e 'first',\n\t\t\t'last_name' =\u003e 'last',\n\t\t\t'is_active' =\u003e true,\n\t\t),\n\t)\n);\n```\n\nAnd deserialize sub model records, even if there are multiple records:\n\n```javascript\n{\n\t\"users\": [\n\t\t{\n\t\t\t\"id\": 1,\n\t\t\t\"username\": \"testusername\",\n\t\t\t\"first_name\": \"first\",\n\t\t\t\"last_name\": \"last\",\n\t\t\t\"is_active\": true,\n\t\t\t\"secondary_models\": {\n\t\t\t\t\"something\": \"blahh\",\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\t\"id\": 2,\n\t\t\t\"username\": \"testusername\",\n\t\t\t\"first_name\": \"first\",\n\t\t\t\"last_name\": \"last\",\n\t\t\t\"is_active\": true,\n\t\t\t\"secondary_models\": [\n\t\t\t\t{\n\t\t\t\t\t\"something\": \"teasdf\",\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"something\": \"fgdfghdfg\",\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t]\n}\n```\n\ninto\n\n```php\n$this-\u003erequest-\u003edata = array(\n\t'User' =\u003e array(\n\t\t0 =\u003e array(\n\t\t\t'id' =\u003e 1,\n\t\t\t'username' =\u003e 'testusername',\n\t\t\t'first_name' =\u003e 'first',\n\t\t\t'last_name' =\u003e 'last',\n\t\t\t'is_active' =\u003e true,\n\t\t\t'SecondaryModel' =\u003e array(\n\t\t\t\t\"something\" =\u003e \"blahh\",\n\t\t\t),\n\t\t),\n\t\t1 =\u003e array(\n\t\t\t'id' =\u003e 2,\n\t\t\t'username' =\u003e 'testusername',\n\t\t\t'first_name' =\u003e 'first',\n\t\t\t'last_name' =\u003e 'last',\n\t\t\t'is_active' =\u003e true,\n\t\t\t'SecondaryModel' =\u003e array(\n\t\t\t\t0 =\u003e array(\n\t\t\t\t\t\"something\" =\u003e \"teasdf\",\n\t\t\t\t),\n\t\t\t\t1 =\u003e array(\n\t\t\t\t\t\"something\" =\u003e \"fgdfghdfg\",\n\t\t\t\t),\n\t\t\t),\n\t\t),\n\t)\n);\n```\n\nIf there is a second top level model in the data to be serialized it is moved\nto a property of the first model\n\n```php\n$data = array(\n\t'User' =\u003e array(\n\t\t'id' =\u003e 1,\n\t\t'username' =\u003e 'testusername',\n\t\t'first_name' =\u003e 'first',\n\t\t'last_name' =\u003e 'last',\n\t\t'is_active' =\u003e true,\n\t),\n\t'SecondModel' =\u003e array(\n\t\t'id' =\u003e 1,\n\t\t'name' =\u003e 'asdflkjasdf'\n\t),\n);\n```\n\ninto:\n\n```javascript\n{\n\t\"user\": {\n\t\t\"id\": 1,\n\t\t\"username\": \"testusername\",\n\t\t\"first_name\": \"first\",\n\t\t\"last_name\": \"last\",\n\t\t\"is_active\": true,\n\t\t\"second_models\": [\n\t\t\t{\n\t\t\t\t'id': 1,\n\t\t\t\t'name': 'asdflkjasdf'\n\t\t\t}\n\t\t]\n\t}\n}\n```\n\nIf there is a second top level model in the data to be deserialized, it is\nignored:\n\n```javascript\n{\n\t\"users\": {\n\t\t\"id\": 1,\n\t\t\"username\": \"testusername\",\n\t\t\"first_name\": \"first\",\n\t\t\"last_name\": \"last\",\n\t\t\"is_active\": true,\n\t},\n\t\"second_models\": {\n\t\t\"id\": 1,\n\t\t\"something\": \"data\"\n\t}\n}\n```\n\ninto\n\n```php\n$this-\u003erequest-\u003edata = array(\n\t'User' =\u003e array(\n\t\t'id' =\u003e 1,\n\t\t'username' =\u003e 'testusername',\n\t\t'first_name' =\u003e 'first',\n\t\t'last_name' =\u003e 'last',\n\t\t'is_active' =\u003e true,\n\t)\n);\n```\n\n## Contributing ##\n\n### Reporting Issues ###\n\nPlease use [GitHub Isuses](https://github.com/loadsys/CakePHP-Serializers/issues) for listing any known defects or issues.\n\n### Development ###\n\nWhen developing this plugin, please fork and issue a PR for any new development.\n\nThe Complete Test Suite for the Plugin can be run via this command:\n\n`./lib/Cake/Console/cake test Serializers AllSerializers`\n\n## License ##\n\n[MIT](https://github.com/loadsys/CakePHP-Serializers/blob/master/LICENSE.md)\n\n\n## Copyright ##\n\n[Loadsys Web Strategies](http://www.loadsys.com) 2015\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floadsys%2Fcakephp-serializers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Floadsys%2Fcakephp-serializers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floadsys%2Fcakephp-serializers/lists"}