Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward an action #1644

Closed
Sybio opened this issue Jan 12, 2018 · 3 comments
Closed

Forward an action #1644

Sybio opened this issue Jan 12, 2018 · 3 comments

Comments

@Sybio
Copy link

Sybio commented Jan 12, 2018

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 :

// ...
class AuditChaptersSaveForAuditAndChapterController extends Controller
{
    /**
     * @Route(
     *     name="api_chapters_save_at_pos",
     *     path="/audit_chapters/save_at_pos/{position}"
     * )
     * @Method("POST")
     * @return Response
     */
    public function createOrUpdateAction(Request $request, $position)
    {
        $request->attributes->add([
            '_api_resource_class' => Chapter::class,
        ]);

       // If chapter already exists at $position
       if ($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.

@soyuka
Copy link
Member

soyuka commented Jan 12, 2018

If it exists already you should have a unique identifier and call PUT /api/chapters/X.

IMO the client should take care of this.

@Sybio
Copy link
Author

Sybio commented Jan 12, 2018

Thanks for reply, ok I should still continue using /POST then /PUT on validation fail...

But let's say I want to forward an Api platform action in a custom action, how can i do ?

@Simperfit
Copy link
Contributor

You can redirect in an event subscribers if you want to, but like @soyuka said it's better to let the client handle this kind of things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants