3

I just started using the dunglas api platform. Im using v2.0.0-rc1 and i added an custom operation to enabled/disable an user.

This is my custom action for the user

<?php

namespace Zoef\UserBundle\Action;

use Zoef\UserBundle\Entity\User;
use Doctrine\Common\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\Routing\Annotation\Route;

class UserAction
{
    /**
     * @Route(
     *     name="enabled_user",
     *     path="/users/{id}/enabled",
     *     defaults={"_api_resource_class"=User::class, "_api_item_operation_name"="enabled"}
     * )
     * @Method("PUT")
     */
    public function __invoke(User $user)
    {
        if($user->isEnabled()) {
            $user->setEnabled(false);
        } else {
            $user->setEnabled(true);
        }

        return $user;
    }
}

When i go to my docs the custom operation is added and functional but to use this action i need to send 4 parameters: email, fullname, username, enabled. but i only want to send the enabled parameter and the id of the user is given in the route but i cant find in the doc how to change the parameters.

Can someone help me with this?

2
  • Are you getting a validation error? Can you post the result of the query PUT? Commented Oct 18, 2016 at 15:18
  • Did you find the solution to this? Commented Jan 2, 2017 at 0:04

1 Answer 1

2

I was trying to make the same enable/disabled and I did it this way:

I created a custom controller in AppBundle\Controller\AddressController

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class AddressController extends Controller
{
    public function enableAction($data)
    {
        $data->setActive(true);

        $em = $this->getDoctrine()->getManager();
        $em->persist($data);
        $em->flush();

        return $data;
    }
}

In my routing.yml I have:

address_enable:
    path:     '/addresses/{id}/enable'
    methods:  ['PUT']
    defaults:
            _controller: 'AppBundle:Address:enable'
            _api_resource_class: 'AppBundle\Entity\Address'
            _api_item_operation_name: 'enable'

In my entity, I have:

 * @ApiResource(
 *     itemOperations={
 *          "enable"={"route_name"="address_enable"},
 *     }
 * )

And after that I just send it as URL/addresses/123/enable no need to send more parameters, just the id.

Sign up to request clarification or add additional context in comments.

1 Comment

This method is not recommended by ApiPlatform (api-platform.com/docs/core/controllers/#alternative-method)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.