使用 GET 方法为集合创建自定义路由
Create a custom route for a collection using GET method
我无法为集合创建自定义路由,我的实体名为 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"})
* @ORM\Entity
* @ORM\Table(name="cases")
*/
控制器文件
<?php
namespace App\Controller;
use App\Entity\File;
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
,我设法在我的控制器中找到解决方案
命名空间App\Controller;
class FileStatsController
{
public function __invoke($data)
{
return $data;
}
}
我设法正确检索了数据
此外,ApiRessource注解中get方法是必须的
collectionOperations={
"get",
* "stats"={
* "method"="GET",
* "path"="/stats",
* "controller"=FileStatsController::class,
* }
* }
我无法为集合创建自定义路由,我的实体名为 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"})
* @ORM\Entity
* @ORM\Table(name="cases")
*/
控制器文件
<?php
namespace App\Controller;
use App\Entity\File;
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
,我设法在我的控制器中找到解决方案
命名空间App\Controller;
class FileStatsController
{
public function __invoke($data)
{
return $data;
}
}
我设法正确检索了数据
此外,ApiRessource注解中get方法是必须的
collectionOperations={
"get",
* "stats"={
* "method"="GET",
* "path"="/stats",
* "controller"=FileStatsController::class,
* }
* }