Skip to content

custom POST #217

@nlefebvre1410

Description

@nlefebvre1410

Hi
I try to personalize the post in an action but it asks me an id?

namespace AppBundle\Action;

use AppBundle\Entity\Book;
use Doctrine\Common\Persistence\ManagerRegistry;
use GuzzleHttp\Psr7\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;

class BookSpecial
{
    private $myService;
    /**
     * @var Container
     */
    private $container;

    public function __construct(MyService $myService,Container $container, AuthorizationChecker $checker)
    {
        $this->myService = $myService;
        $this->container = $container;
        $this->authorizationChecker = $checker;
    }

    /**
     * @Route(
     *     name="book_special",
     *     path="api/books/special",
     *     defaults={"_api_resource_class"=Book::class, "_api_item_operation_name"="special"},
     *     options={"i18n" = false }
     * )
     * @Method("POST")
     */
    public function __invoke($data)
    {

        if (!$this->authorizationChecker->isGranted('ROLE_ADMIN')) {
            $data =  array('error'=>410);
            return new JsonResponse($data, 400);
    }
        $this->myService->doSomething($data);


        return $data; // API Platform will automatically validate, persist (if you use Doctrine) and serialize an entity
        // for you. If you prefer to do it yourself, return an instance of Symfony\Component\HttpFoundation\Response
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions