我没有设法为集合创建自定义路由,我的实体名为File。
这是我的实体注释:
/**
* @ApiResource(
*
* normalizationContext={"groups"={"file"},"enable_max_depth"=true},
* denormalizationContext={"groups"={"file-write-customers"},"enable_max_depth"=true},
* attributes={"force_eager"=false},
* itemOperations={
* "get",
* "put",
* "get_mandate_pdf"={
* "method"="POST",
* "path"="/files/{id}/mandate-pdf",
* "controller"=FileCreatePdfController::class,
* },
* },
* collectionOperations={
* "stats"={
* "method"="GET",
* "path"="/files/stats",
* "controller"=FileStatsController::class,
* }
* },
* )
* @ApiFilter(SearchFilter::class, properties={"status": "exact", "sponsor": "exact"})
* @ApiFilter(DateFilter::class, properties={"updatedAt"})
* @ORMEntity
* @ORMTable(name="cases")
*/
控制器文件
<?php
namespace AppController;
use AppEntityFile;
class FileStatsController
{
public function __invoke(File $data): File
{
return $data;
}
}
但是当我到达/files/stats 时我遇到了此错误,似乎 api 平台期待一个 Id . 出于某些原因,如果我将方法从 GET 切换到 POST,则路由正在工作
{
"@context": "/contexts/Error",
"@type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "The identifier id is missing for a query of App\Entity\File",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/srv/api/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php",
"line": 309,
"args": []
},
如果我删除类型变量,我设法在我的控制器中找到解决方案 $data
命名空间应用\控制器;
class FileStatsController
{
public function __invoke($data)
{
return $data;
}
}
我设法正确检索数据
此外,方法get在ApiRessource注释中是必需的。
collectionOperations={
"get",
* "stats"={
* "method"="GET",
* "path"="/stats",
* "controller"=FileStatsController::class,
* }
* }