{"id":19258927,"url":"https://github.com/lassehaslev/api-response","last_synced_at":"2026-06-12T22:32:31.086Z","repository":{"id":57012594,"uuid":"63013794","full_name":"LasseHaslev/api-response","owner":"LasseHaslev","description":"Simplified syntax for adding content to your JSON-api.","archived":false,"fork":false,"pushed_at":"2016-12-21T06:59:47.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-05T09:29:17.847Z","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/LasseHaslev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-10T18:53:48.000Z","updated_at":"2017-07-27T10:40:16.000Z","dependencies_parsed_at":"2022-08-21T15:10:31.234Z","dependency_job_id":null,"html_url":"https://github.com/LasseHaslev/api-response","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LasseHaslev%2Fapi-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LasseHaslev%2Fapi-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LasseHaslev%2Fapi-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LasseHaslev%2Fapi-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LasseHaslev","download_url":"https://codeload.github.com/LasseHaslev/api-response/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240356192,"owners_count":19788513,"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-09T19:14:49.378Z","updated_at":"2026-06-12T22:32:31.036Z","avatar_url":"https://github.com/LasseHaslev.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# api-response\n\u003e Helper class for giving JSON api response\n\n## Motivation\nIts hard to allways write api response for everyting, and this package will make the job much easier.\n\nThis package and its base concept is greatly inspired by [dingo/api](https://github.com/dingo/api).\nAnd uses most of the same transformer syntax as [Fractal](http://fractal.thephpleague.com/transformers/).\n\n## Install\nI use this package mainly in my [Laravel](https://laravel.com/) projects.\n\nRun ```composer require lassehaslev/api-response``` in your project folder\n\n## Usage\n#### Responder\nThe Responder is where we call what items we want transformed to JsonResponse. This class need to implement the ```ResponseTrait``` and will then make the ```$this-\u003eresponse``` object available. This will make it posible to use ```$response-\u003eitem()``` and ```$response-\u003ecollection()```.\nThe first argument for both functions are the data to be transformed, the second argument is the transformer to transform the data. Read below for more information.\n``` php\nuse LasseHaslev\\ApiResponse\\Responses\\ResponseTrait;\nclass ResponseCaller\n{\n    use ResponseTrait;\n\n    public function getItem() {\n        return $this-\u003eresponse-\u003eitem( [ 'name'=\u003e'Test name' ], new Transformer );\n    }\n\n    public function getCollection() {\n        return $this-\u003eresponse-\u003ecollection( [\n            [ 'name'=\u003e'Test name' ],\n            [ 'name'=\u003e'Another test name' ]\n        ], new Transformer );\n    }\n\n}\n```\n\n#### Transformer\nThe transformer is where we format the data to the api.\nAll you have to do is to create a ```public function transform``` with the model you want to transformed.\nThen you return an ```array``` with the data.\n\nYou can also have the transformer include more data. This can be useful when model has data. \nThis is done by two different types ```default``` and ```available```, but both types works the same way:\nUpdate the array of the type example: ```protected $defaultIncludes = ['name']```.\nThen you need to create a public function thats has name prefix ```include```. Example: ```public function includeName( $model )```.\n\n###### Default includes\nThe default include will be included by default.\n\n###### Available includes\nThe available include will only be included when url parameter ```include``` are provided. Example ```/api/request?include=name```\n\nIf you have multiple available includes you can include them by having url request like ```/api/request?include=first,second```\n\n``` php\nuse LasseHaslev\\ApiResponse\\Transformers\\Transformer as BaseTransformer;\nclass Transformer extends BaseTransformer\n{\n\n    protected $defaultIncludes = [ 'default' ];\n    protected $availableIncludes = [ 'available' ];\n\n    /**\n     * Transform $model\n     *\n     * @return Array\n     */\n    public function transform( $model )\n    {\n        return [\n            'name'=\u003e$model[ 'name' ],\n        ];\n    }\n\n    /**\n     * Return include default\n     *\n     * @return Array\n     */\n    public function includeDefault($model)\n    {\n        return [ 'name'=\u003e'Include default' ];\n    }\n\n    /**\n     * Return include available\n     *\n     * @return Array\n     */\n    public function includeAvailable($model)\n    {\n        return [ 'name'=\u003e'Include available' ];\n    }\n\n}\n```\n\n## License\nMIT, dawg\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flassehaslev%2Fapi-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flassehaslev%2Fapi-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flassehaslev%2Fapi-response/lists"}