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