{"id":36996145,"url":"https://github.com/mattjanssen/api-response-bundle","last_synced_at":"2026-01-13T23:48:30.416Z","repository":{"id":62525268,"uuid":"44984097","full_name":"mattjanssen/api-response-bundle","owner":"mattjanssen","description":"Symfony bundle for transforming controller action returns and exceptions into JSON API responses.","archived":false,"fork":false,"pushed_at":"2019-12-04T07:39:50.000Z","size":88,"stargazers_count":7,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-09T14:17:52.972Z","etag":null,"topics":["bundle","json-response","php","symfony","symfony-bundle"],"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/mattjanssen.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":"2015-10-26T17:04:22.000Z","updated_at":"2024-05-08T11:44:05.000Z","dependencies_parsed_at":"2022-11-02T14:15:51.222Z","dependency_job_id":null,"html_url":"https://github.com/mattjanssen/api-response-bundle","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/mattjanssen/api-response-bundle","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattjanssen%2Fapi-response-bundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattjanssen%2Fapi-response-bundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattjanssen%2Fapi-response-bundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattjanssen%2Fapi-response-bundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattjanssen","download_url":"https://codeload.github.com/mattjanssen/api-response-bundle/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattjanssen%2Fapi-response-bundle/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28405417,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["bundle","json-response","php","symfony","symfony-bundle"],"created_at":"2026-01-13T23:48:30.293Z","updated_at":"2026-01-13T23:48:30.398Z","avatar_url":"https://github.com/mattjanssen.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mattjanssen/api-response-bundle\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE.md)\n[![Build Status](https://travis-ci.org/mattjanssen/api-response-bundle.svg?branch=master)](https://travis-ci.org/mattjanssen/api-response-bundle)\n\n`mattjanssen/api-response-bundle` is a slightly-opinionated Symfony bundle for transforming controller action returns\nand exceptions into a standardized JSON response. The serializer and CORS headers can be configured globally, per path,\nand per action via annotation.\n\n## Install\n\nVia Composer\n\n``` bash\n$ composer require mattjanssen/api-response-bundle\n```\n\n## Enable\n\nEnable the bundle in the kernel:\n\n``` php\n\u003c?php\n// app/AppKernel.php\n\npublic function registerBundles()\n{\n    $bundles = [\n        // ...\n        new MattJanssen\\ApiResponseBundle\\ApiResponseBundle(),\n    ];\n}\n```\n\n## Configure\n\n``` yml\napi_response:\n    defaults:\n        serializer: json_encode\n        serialize_groups: []\n        cors_allow_origin_regex: https://.*\\.mydomain\\.com\n        cors_allow_headers: [Authorization, Content-Type]\n        cors_max_age: 86400\n    paths:\n        somename:\n            prefix: /api/v1/\n            serializer: jms_serializer\n        othername:\n            pattern: ^/api/v[2-4]/\n            cors_allow_origin_regex: .*\n```\n\nThe serializer can be empty, 'array', 'json_encode', 'json_group_encode', 'jms_serializer',\nor the name of a service which must implement the SerializerAdapterInterface. It defaults to 'json_encode'.\n\n## Usage\n\nIn your API controllers, just return whatever you want serialized in the response. The ApiResponseBundle takes care of\nturning that into an actual JSON response.\n\n``` php\nreturn [\n    'id' =\u003e 5,\n    'school' =\u003e $school,\n    'users' =\u003e $users,\n];\n```\n\nThis would result in the following JSON return:\n\n``` javascript\n{\n    data: {\n        id: 5,\n        school: ...,\n        users: [ ... ]\n    },\n    error: null\n}\n```\n\n## Status Codes\n \nBy default, responses are sent with the 200 OK status. In order to use a different status, use the `@ApiResponse` \nannotation on the controller action. This should only be used to change the success status codes. See the Error Response\nsection for handling error output.\n\n``` php\n/**\n * @ApiResponse(httpCode=201)\n */\npublic function createAction() {}\n```\n\nThe resulting response would have the 201 CREATED status.\n\n## Error Response\n\nTo respond with an error, throw any exception implementing the `ApiResponseExceptionInterface`. On the exception you can\noptionally set the HTTP status code, the exception code, the exception message and the error data to be serialized into the \nresponse.\n\n``` php\nthrow (new ApiResponseException())\n    -\u003esetHttpStatusCode(404)\n    -\u003esetCode(100404)\n    -\u003esetMessage('Could not find school.')\n    -\u003esetErrorData(['schoolId' =\u003e 42]);\n```\n\nThis would result in the following JSON return with a 404 HTTP status:\n\n``` javascript\n{\n    data: null,\n    error: {\n        code: 100404,\n        message: 'Could not find school.',\n        errorData: {\n            schoolId: 42\n        }\n    }\n}\n```\n\n## Exception Handling\n\nBesides turning `ApiResponseExceptionInterface` exceptions into error responses, the bundle will also handle any\nuncaught exceptions in the following manner:\n\n### `HttpExceptionInterface`\n\nThe exception status code is used for both the response HTTP code and the error code. The error message is the\ncorresponding `Response::$statusTexts` array value. Error data is null.\n\n### `AuthenticationException`\n\nBoth the response HTTP code and the error code are 401. The error message is \"Unauthorized\". Error data is null.\n\n### `AccessDeniedException`\n\nBoth the response HTTP code and the error code are 403. The error message is \"Forbidden\". Error data is null.\n\n### All Other Exceptions\n\nBoth the response HTTP code and the error code are 500.\n\nIf the Symfony kernel *is not in debug mode*, the error message is \"Internal Server Error\". Error data is null.\n\nIf the Symfony kernel *is in debug mode*, the error message is compiled from the exception class, message, file and \nline number. And the error data is the exception trace.\n\n## Testing\n\n``` bash\n$ composer install --dev\n$ vendor/bin/phpunit\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/mattjanssen/api-response-bundle.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/mattjanssen/api-response-bundle\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattjanssen%2Fapi-response-bundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattjanssen%2Fapi-response-bundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattjanssen%2Fapi-response-bundle/lists"}