Symfony 如何从api平台返回自定义数据?

Symfony 如何从api平台返回自定义数据?,symfony,symfony4,api-platform.com,Symfony,Symfony4,Api Platform.com,我是symfony和api平台的初学者。我尝试实现一个允许返回自定义数据而不是api平台响应的解决方案 我尝试过,但无法实施/管理它 这是我的密码: 应用程序\实体中的Task.php 以下是调用endpoint时我想返回的答案: {成功:true,消息:成功发送} 我希望这是清楚的,如果没有,请让我知道,感谢advance,因为Api平台不是为这些事情而设计的,您必须通过隐式添加资源定义作为请求属性来欺骗它: /** *@路线 *名称=任务, *路径=/task, *方法={POST,OPTI

我是symfony和api平台的初学者。我尝试实现一个允许返回自定义数据而不是api平台响应的解决方案

我尝试过,但无法实施/管理它

这是我的密码:

应用程序\实体中的Task.php

以下是调用endpoint时我想返回的答案:

{成功:true,消息:成功发送}


我希望这是清楚的,如果没有,请让我知道,感谢advance

,因为Api平台不是为这些事情而设计的,您必须通过隐式添加资源定义作为请求属性来欺骗它:

/** *@路线 *名称=任务, *路径=/task, *方法={POST,OPTIONS}, *默认值={ *\u api\u资源\u类=任务::类, *\u api\u集合\u操作\u名称=post * } * *@param任务$data *@return-JsonResponse */ 公共函数调用任务$data:JsonResponse{ 返回新的JsonResponse'task success json',201; }
post需要在collectionOperations中,而不是ItemOperations中感谢您的回答,现在我得到了->App\\Controller\\TaskController的返回值::\uu invoke必须是App\\Entity\\Task的实例,看起来我只能返回Task的实例,而没有自定义返回实际上您需要欺骗它才能工作,所以这将是一种糟糕的做法,因为它不是为了按照你想要的方式工作,尽管我和你有过类似的案例,但我非常感谢你的回答和帮助!它起作用了!
<?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;
    }


}
<?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;
    }
}
{
    "@context": "\/api\/contexts\/Task",
    "@id": "\/api\/tasks\/17",
    "@type": "Task",
    "user": null,
    "id": 17,
    "name": "string",
    "time": "string",
    "priority": "string"
}