{"id":16233436,"url":"https://github.com/alcalyn/serializable-api-response","last_synced_at":"2025-04-08T07:36:26.296Z","repository":{"id":56943839,"uuid":"60028266","full_name":"alcalyn/serializable-api-response","owner":"alcalyn","description":"Allows to return raw object as controller response so that it will be serialized later by serializer and converted to Symfony Response.","archived":false,"fork":false,"pushed_at":"2018-04-25T11:07:35.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-14T04:47:46.925Z","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/alcalyn.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}},"created_at":"2016-05-30T17:40:52.000Z","updated_at":"2018-04-25T11:06:54.000Z","dependencies_parsed_at":"2022-08-21T07:50:55.328Z","dependency_job_id":null,"html_url":"https://github.com/alcalyn/serializable-api-response","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcalyn%2Fserializable-api-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcalyn%2Fserializable-api-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcalyn%2Fserializable-api-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcalyn%2Fserializable-api-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alcalyn","download_url":"https://codeload.github.com/alcalyn/serializable-api-response/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247797276,"owners_count":20997706,"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-10-10T13:12:39.948Z","updated_at":"2025-04-08T07:36:26.250Z","avatar_url":"https://github.com/alcalyn.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Serializable Api Response\n\nIf you use Symfony Http foundation and JMS Serializer,\nthis library allows to return raw objects and status code as a controller response\nso that it will be serialized later by serializer and converted to a Symfony Response.\n\n**The problem**: Symfony converts response content to string if we use Symfony Response,\nor if we return a raw object and use a filter listener, then we can't pass status code.\n\n\n## Installation\n\nInstall via composer\n\n``` js\n{\n    \"require\": {\n        \"alcalyn/serializable-api-response\": \"~1.0\"\n    }\n}\n```\n\n\n## Usage\n\nReturn a non-yet-serialized response in your controller:\n\n``` php\nuse Symfony\\Component\\HttpKernel\\Exception\\ConflictHttpException;\nuse Symfony\\Component\\HttpFoundation\\Request;\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Alcalyn\\SerializableApiResponse\\ApiResponse;\nuse Acme\\UserApi\\Exception\\UserAlreadyExistsException;\n\nclass UserController\n{\n    /**\n     * @param Request $request\n     *\n     * @return ApiResponse\n     *\n     * @throws ConflictHttpException if username already exists.\n     */\n    public function registerUserAction(Request $request)\n    {\n        $username = $request-\u003erequest-\u003eget('username');\n        $password = $request-\u003erequest-\u003eget('password');\n\n        try {\n            $user = $this-\u003euserManager-\u003ecreateUser($username, $password);\n        } catch (UserAlreadyExistsException $e) {\n            throw new ConflictHttpException('An user with username \"'.$username.'\" already exists.', $e);\n        }\n\n        return new ApiResponse($user, Response::HTTP_CREATED);\n    }\n}\n```\n\nRegister ApiResponse Filter Listener:\n\nIn Silex:\n\n``` php\nuse Symfony\\Component\\HttpKernel\\KernelEvents;\nuse Alcalyn\\SerializableApiResponse\\ApiResponseFilter;\n\n// Register reponse filter as a service\n$this['acme.listener.api_response_filter'] = function () {\n    $serializer = $this['serializer']; // Assuming your serializer service has this name\n\n    return new ApiResponseFilter($serializer);\n};\n\n// Listen Kernel response to convert ApiResponse with raw object to Symfony Response with serialized data\n$this-\u003eon(KernelEvents::VIEW, function ($event) {\n    $this['acme.listener.api_response_filter']-\u003eonKernelView($event);\n});\n```\n\n\n## License\n\nThis project is under [MIT Lisense](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falcalyn%2Fserializable-api-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falcalyn%2Fserializable-api-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falcalyn%2Fserializable-api-response/lists"}