I have a model class
user and I have three API end points related to this user object
- POST /api/user
- GET /api/user/:id_user
- PUT /api/user/
The POST and PUT has JSON body. I want to return the
user object as HAL response for all three operations. My three handlers are looks like the following:
$userObj = $this->userService->relevent_service($params); // returning an user object $request = $request->withAttribute("id_user", $userObj->getId()); $resource = $this->resourceGenerator->fromObject($eventObj,$request); return $this->responseFactory->createResponse($request, $resource);
My halMetadataMap is like the following:
[ '__class__' => RouteBasedResourceMetadata::class, 'resource_class' => user::class, 'route' => 'api.getUserById', 'extractor' => ArraySerializableHydrator::class, ]
It returns the right user HAL object with the GET route without any issue, but when I tried to generate the HAL object with POST and PUT route, the
ResourceGenerator throws an exception.
If I create two additional proxy class for user class and create three separate metadata map for POST and PUT, it works without any issue. but it seems redundant. And the scenario I describe is not so uncommon. Therefore, what is the best practice to achieve this output?