{"id":13684509,"url":"https://github.com/andrej-griniuk/cakephp-fractal-transformer-view","last_synced_at":"2025-04-30T21:30:45.388Z","repository":{"id":54535662,"uuid":"51300279","full_name":"andrej-griniuk/cakephp-fractal-transformer-view","owner":"andrej-griniuk","description":"CakePHP view builder utilizing Fractal library for entities transformation","archived":false,"fork":false,"pushed_at":"2023-12-11T04:55:10.000Z","size":50,"stargazers_count":19,"open_issues_count":0,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-06T11:19:21.411Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/andrej-griniuk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-02-08T13:46:29.000Z","updated_at":"2024-07-26T15:25:17.000Z","dependencies_parsed_at":"2022-08-13T19:00:47.739Z","dependency_job_id":"8f3a598a-6fc5-44d5-886c-34e2c48a772d","html_url":"https://github.com/andrej-griniuk/cakephp-fractal-transformer-view","commit_stats":{"total_commits":22,"total_committers":4,"mean_commits":5.5,"dds":"0.13636363636363635","last_synced_commit":"cf85d3b379810d3c655f658f49ec7742523b121e"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrej-griniuk%2Fcakephp-fractal-transformer-view","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrej-griniuk%2Fcakephp-fractal-transformer-view/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrej-griniuk%2Fcakephp-fractal-transformer-view/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrej-griniuk%2Fcakephp-fractal-transformer-view/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrej-griniuk","download_url":"https://codeload.github.com/andrej-griniuk/cakephp-fractal-transformer-view/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224224792,"owners_count":17276428,"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-08-02T14:00:34.308Z","updated_at":"2024-11-12T05:32:20.004Z","avatar_url":"https://github.com/andrej-griniuk.png","language":"PHP","funding_links":[],"categories":["REST and API","Plugins"],"sub_categories":["REST and API"],"readme":"[![Build Status](https://app.travis-ci.com/andrej-griniuk/cakephp-fractal-transformer-view.svg?branch=master)](https://travis-ci.org/andrej-griniuk/cakephp-fractal-transformer-view)\n[![codecov](https://codecov.io/gh/andrej-griniuk/cakephp-fractal-transformer-view/branch/master/graph/badge.svg)](https://codecov.io/gh/andrej-griniuk/cakephp-fractal-transformer-view)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)\n\n# FractalTransformerView plugin for CakePHP\n\nThis plugin is a thin wrapper for `JsonView` that allows using [Fractal transformers][fractal-transformer] for your API output. What is [Fractal][fractal]?\n\n\u003e Fractal provides a presentation and transformation layer for complex data output, the like found in RESTful APIs, and works really well with JSON. Think of this as a view layer for your JSON/YAML/etc.\n\u003e When building an API it is common for people to just grab stuff from the database and pass it to json_encode(). This might be passable for “trivial” APIs but if they are in use by the public, or used by mobile applications then this will quickly lead to inconsistent output.\n\n\n## Requirements\n\n- CakePHP 5.x (use ~1.0 for CakePHP 3.x, ~2.0 for CakePHP 4.x)\n\n## Installation\n\nYou can install this plugin into your CakePHP application using [Composer][composer].\n\n```bash\ncomposer require andrej-griniuk/cakephp-fractal-transformer-view\n```\n\n## Usage\nTo enable the plugin set `FractalTransformerView.FractalTransformer` class name for viewBuilder. Then you just do what you would normally do in your [data views](http://book.cakephp.org/4/en/views/json-and-xml-views.html) - specify which view vars you want to get serialized by setting `serialize` view builder option. E.g.:\n\n```php\nnamespace App\\Controller;\n\nclass ArticlesController extends AppController\n{\n    public function initialize(): void\n    {\n        parent::initialize();\n        \n        $this-\u003eloadComponent('RequestHandler');\n        \n        $this-\u003eviewBuilder()-\u003esetClassName('FractalTransformerView.FractalTransformer');\n    }\n\n    public function index()\n    {\n        // Set the view vars that have to be serialized.\n        $this-\u003eset('articles', $this-\u003epaginate());\n        // Specify which view vars JsonView should serialize.\n        $this-\u003eviewBuilder()-\u003esetOption('serialize', ['articles']);\n    }\n}\n```\n\nThe view will look for transformer class starting with entity name. E.g.:\n\n```php\nnamespace App\\Model\\Transformer;\n\nuse App\\Model\\Entity\\Article;\nuse League\\Fractal\\TransformerAbstract;\n\nclass ArticleTransformer extends TransformerAbstract\n{\n    /**\n     * Creates a response item for each instance\n     *\n     * @param Article $article post entity\n     * @return array transformed post\n     */\n    public function transform(Article $article)\n    {\n        return [\n            'title' =\u003e $article-\u003eget('title')\n        ];\n    }\n}\n```\n\nIf transformer class not found the variable is serialized the normal way.\n\nCustom transformer class name can be set by defining `transformer` view builder option:\n\n```php\n$this-\u003eviewBuilder()-\u003esetOption('transform', ['articles' =\u003e '\\App\\Model\\Transformer\\CustomArticleTransformer']);\n```\n\nYou can also define if you don't want to use transformer for certain variables:\n\n```php\n$this-\u003eviewBuilder()-\u003esetOption('transform', ['articles' =\u003e false]);\n```\n\nYou can set a custom serializer (class name or object) via `serializer` view builder option:\n\n```php\n$this-\u003eviewBuilder()-\u003esetOption('serializer', new CustomSerializer());\n```\n## Baking Transformers\n\nTo bake transformers you must include the plugin in your src/Application.php file. Add the following to your bootstrap method:\n\n```php\n$this-\u003eaddPlugin('FractalTransformerView');\n```\n\nYou must also have the [cakephp/bake](https://packagist.org/packages/cakephp/bake) composer package installed.\n\nYou can now run `bin/cake bake transformer YOUR_MODEL` to create transformers.\n\n## Bugs \u0026 Feedback\n\nhttps://github.com/andrej-griniuk/cakephp-fractal-transformer-view/issues\n\n## Credits\n\nInspired by @josegonzalez [Using Fractal to transform entities for custom api endpoints](http://josediazgonzalez.com/2015/12/01/using-fractal-to-transform-entities-for-custom-api-endpoints/).\n\n## License\n\nCopyright (c) 2016, [Andrej Griniuk][andrej-griniuk] and licensed under [The MIT License][mit].\n\n[cakephp]:http://cakephp.org\n[composer]:http://getcomposer.org\n[fractal]:http://fractal.thephpleague.com/\n[fractal-transformer]:http://fractal.thephpleague.com/transformers/\n[mit]:http://www.opensource.org/licenses/mit-license.php\n[andrej-griniuk]:https://github.com/andrej-griniuk\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrej-griniuk%2Fcakephp-fractal-transformer-view","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrej-griniuk%2Fcakephp-fractal-transformer-view","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrej-griniuk%2Fcakephp-fractal-transformer-view/lists"}