2

I’m beginner in symfony and api-platform. I try to implement a solution that allow to return custom data instead of api-platform response.

I try custom controller and HttpFoundation\Response but unable to implement/manage it.

Here is my code :

Task.php in APP\Entity

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\TaskController;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource(itemOperations={
 *     "get",
 *     "post_publication"={
 *         "method"="POST",
 *         "path"="/tasks",
 *         "controller"=TaskController::class,
 *     "read"=false,
 *     }
 * })
 * @ORM\Entity(repositoryClass="App\Repository\TaskRepository")
 */
class Task
{
    /**
     * @Groups("tasks")
     */

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */ 
    private $name;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $time;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $priority;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     */
    public  $user;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getTime(): ?string
    {
        return $this->time;
    }

    public function setTime(string $time): self
    {
        $this->time = $time;

        return $this;
    }

    public function getPriority(): ?string
    {
        return $this->priority;
    }

    public function setPriority(string $priority): self
    {
        $this->priority = $priority;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser($user): void
    {
        $this->user = $user;
    }


}

TaskController.php in App\Controller

<?php

namespace App\Controller;

use App\Entity\Task;
use Symfony\Component\HttpFoundation\Response;


class TaskController extends Task
{
    public function __construct()
    {
    }

    public function __invoke(Task $data): Task
    {

        $result["success"] = true;

        return $result;
    }
}

Here is the answer i get after a post method on api/tasks

{
    "@context": "\/api\/contexts\/Task",
    "@id": "\/api\/tasks\/17",
    "@type": "Task",
    "user": null,
    "id": 17,
    "name": "string",
    "time": "string",
    "priority": "string"
}

Here is the answer i would like to return when endpoint is call :

{ "success": true, "message": "successfully send" }

I hope that it's clear, if not, let me know, thanks by advance

3
  • 1
    post needs to be in collectionOperations, not itemOperations Commented Jul 29, 2019 at 13:59
  • Thank's for the answer, now i got -> Return value of App\\Controller\\TaskController::__invoke() must be an instance of App\\Entity\\Task , look like i only can return an instance of Task and none of custom return Commented Jul 29, 2019 at 14:04
  • 1
    In fact you need to cheat it to work, so this will be bad practice, because it is not intended to work the way you want (although I had similar case like you once) Commented Jul 29, 2019 at 14:27

1 Answer 1

4

Since Api-platform is not designed for such things you have to cheat it by implicitly adding the resource definitions as request attributes:

    /**
     * @Route(
     *     name="task",
     *     path="/task",
     *     methods={"POST","OPTIONS"},
     *     defaults={
     *          "_api_resource_class"=Task::class,
     *          "_api_collection_operation_name"="post"
     *     }
     * )
     * @param Task $data
     * @return JsonResponse
     */
    public function __invoke(Task $data): JsonResponse {
        return new JsonResponse('task success json', 201);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the answer and for your help ! It's working !

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.