You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my case the front app calls multiple times POST/chapters.
Sometimes, chapters already exist for given unique property $chapter->position and I would like to update and redirect to PUT instead of returning an error saying the object already exists. I want to implement this behavior to prevent too many calls (If /POST refused, currently need to call /PUT, multiple times...).
And to avoid duplicate code or process, i don't want to design custom code to update or save my object $chapter, i want to delegate to native routes POST or PUT that are already configured (with annotations, groups and validators).
So my question, is there a way to forward an Api platform action in a custom controller (or action), like that :
// ...classAuditChaptersSaveForAuditAndChapterControllerextendsController
{
/** * @Route( * name="api_chapters_save_at_pos", * path="/audit_chapters/save_at_pos/{position}" * ) * @Method("POST") * @return Response */publicfunctioncreateOrUpdateAction(Request$request, $position)
{
$request->attributes->add([
'_api_resource_class' => Chapter::class,
]);
// If chapter already exists at $positionif ($chapter = $chapterRepository->findOneByPosition($position)) {
// Forward native PUT action to update the existing chapter$response = $this->forward('api_platform.action.put_item', [
'request' => $request,
'id' => $chapter->getId(),
]);
} else {
// Else forward POST action to create new one$response = $this->forward('api_platform.action.post_collection', [
'request' => $request,
]);
}
return$response;
}
}
I tried above code without success, got :
Controller "ApiPlatform\Core\Action\PlaceholderAction" requires that you provide a value for the "$data" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.
Please note I found interesting stuff for previous API Platform versions, like #477 (comment) and #477 (comment)
But neither ResourceController or DunglasApiBundle still exist on v2.1.3.
The text was updated successfully, but these errors were encountered:
In my case the front app calls multiple times
POST
/chapters
.Sometimes, chapters already exist for given unique property
$chapter->position
and I would like to update and redirect toPUT
instead of returning an error saying the object already exists. I want to implement this behavior to prevent too many calls (If /POST refused, currently need to call /PUT, multiple times...).And to avoid duplicate code or process, i don't want to design custom code to update or save my object
$chapter
, i want to delegate to native routesPOST
orPUT
that are already configured (with annotations, groups and validators).So my question, is there a way to forward an Api platform action in a custom controller (or action), like that :
I tried above code without success, got :
Please note I found interesting stuff for previous API Platform versions, like #477 (comment) and #477 (comment)
But neither
ResourceController
orDunglasApiBundle
still exist on v2.1.3.The text was updated successfully, but these errors were encountered: