我使用api平台文档中编写的方法创建了一个POST类型的个性化操作。系统正常运行,但函数__ invoke()的对象 $ data保持空(此对象的所有属性都为空)
这是Step类的代码:
namespace App\Entity\Api;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\Step\Create as StepCreate;
/**
* Class Step
* @ApiResource(itemOperations={
* "get",
* "special"={
* "method"="POST",
* "path"="/step/create",
* "controller"=StepCreate::class,
* "defaults"={"_api_receive"=false},
* }
* })
*
* @package App\Entity\Api\Step
*/
class Step {
/**
* @var null|string
*/
private $title;
/**
* @var null|string
*/
private $description;
/**
* @var float
*/
private $latitude;
/**
* @var float
*/
private $longitude;
/**
* @var int
*/
private $tripId;
...
}
以及控制器的代码:
namespace App\Controller\Api\Step;
use App\Entity\Api\Step;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class Create {
/**
* @param \App\Entity\Api\Step $data
* @return mixed
*/
public function __invoke(Step $data) {
dump($data);
return $data;
}
}
这是转储($ data)的结果:
<pre>
<abbr title="App\Entity\Api\Step">Step</abbr> {
<samp>
-
<span title="Private property defined in class: `App\Entity\Api\Step`">title</span>:
<span class=sf-dump-const>null</span>
-
<span title="Private property defined in class: `App\Entity\Api\Step`">description</span>:
<span class=sf-dump-const>null</span>
-
<span title="Private property defined in class: `App\Entity\Api\Step`">latitude</span>:
<span class=sf-dump-const>null</span>
-
<span title="Private property defined in class: `App\Entity\Api\Step`">longitude</span>:
<span class=sf-dump-const>null</span>
-
<span title="Private property defined in class: `App\Entity\Api\Step`">tripId</span>:
<span class=sf-dump-const>null</span>
</samp>}
</pre>
有人可以帮我吗?
谢谢:)
答案 0 :(得分:0)
这是预期的行为。如果您要获取Step
对象,则需要摆脱"defaults"={"_api_receive"=false},
请参阅https://api-platform.com/docs/core/operations#entity-retrieval