使用API平台在symfony 5中创建子资源路由
我正在尝试在我的symfony应用程序中声明子资源。我遵循了api平台文档: 子资源确实出现在路由中,但不在其父资源下 我目前拥有的路线:使用API平台在symfony 5中创建子资源路由,symfony,api-platform.com,symfony5,symfony-routing,Symfony,Api Platform.com,Symfony5,Symfony Routing,我正在尝试在我的symfony应用程序中声明子资源。我遵循了api平台文档: 子资源确实出现在路由中,但不在其父资源下 我目前拥有的路线: api_files_get_collection GET ANY ANY /api/files.{_format} api_files_post_collection
api_files_get_collection GET ANY ANY /api/files.{_format}
api_files_post_collection POST ANY ANY /api/files.{_format}
api_files_get_item GET ANY ANY /api/files/{id}.{_format}
api_files_patch_item PATCH ANY ANY /api/files/{id}.{_format}
api_files_put_item PUT ANY ANY /api/files/{id}.{_format}
api_external_subscription_requests_get_collection GET ANY ANY /api/external_subscription_requests.{_format}
api_external_subscription_requests_post_publication_collection POST ANY ANY /api/subscribe
api_external_subscription_requests_get_item GET ANY ANY /api/external_subscription_requests/{id}.{_format}
我想要的路线是:
api_files_get_collection GET ANY ANY /api/files.{_format}
api_files_post_collection POST ANY ANY /api/files.{_format}
api_files_get_item GET ANY ANY /api/files/{id}.{_format}
api_files_patch_item PATCH ANY ANY /api/files/{id}.{_format}
api_files_put_item PUT ANY ANY /api/files/{id}.{_format}
api_files_external_subscription_requests_get_collection GET ANY ANY /api/files/{id}/external_subscription_requests.{_format}
api_files_external_subscription_requests_post_publication_collection POST ANY ANY /api/files/{id}/subscribe
api_files_external_subscription_requests_get_item GET ANY ANY /api/files/{id}/external_subscription_requests/{id}.{_format}
App\Entity\File.php
中的代码:
/**
* @ORM\Entity
* @ApiResource(
* attributes={"order"={"createdAt": "DESC"}},
* collectionOperations={
* "get"={
* "normalization_context"={"groups"={"model:timestampable", "file:collection:read"}},
* },
* "post",
* },
* itemOperations={
* "get"={
* "normalization_context"={"groups"={"model:timestampable", "file:item:read"}},
* },
* "patch",
* "put"
* },
* )
*/
class File
{
// ...
/**
* @ORM\OneToMany(targetEntity="App\Entity\ExternalSubscriptionRequest", cascade={"all"}, mappedBy="file")
* @Groups({
* "file:collection:read",
* "file:item:read"
* })
* @ApiSubresource()
*/
private $external_subscription_requests;
// ...
}
/**
* @ORM\Entity
* @ApiResource(
* collectionOperations={
* "get",
* "post_publication"={
* "method"="POST",
* "path"="/subscribe",
* "controller"=SubscribeToConso::class,
* }
* },
* itemOperations={
* "get",
* },
* )
*/
class ExternalSubscriptionRequest
{
// ...
/**
* @var File the file this request was made for
*
* @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="external_subscription_requests")
* @ORM\JoinColumn(referencedColumnName="id", nullable=false)
*
* @Groups({
* "external_subscription_request:collection:read",
* "external_subscription_request:item:read"
* })
*/
public $file;
// ...
}
App\Entity\ExternalSubscriptionRequest.php中的代码
/**
* @ORM\Entity
* @ApiResource(
* attributes={"order"={"createdAt": "DESC"}},
* collectionOperations={
* "get"={
* "normalization_context"={"groups"={"model:timestampable", "file:collection:read"}},
* },
* "post",
* },
* itemOperations={
* "get"={
* "normalization_context"={"groups"={"model:timestampable", "file:item:read"}},
* },
* "patch",
* "put"
* },
* )
*/
class File
{
// ...
/**
* @ORM\OneToMany(targetEntity="App\Entity\ExternalSubscriptionRequest", cascade={"all"}, mappedBy="file")
* @Groups({
* "file:collection:read",
* "file:item:read"
* })
* @ApiSubresource()
*/
private $external_subscription_requests;
// ...
}
/**
* @ORM\Entity
* @ApiResource(
* collectionOperations={
* "get",
* "post_publication"={
* "method"="POST",
* "path"="/subscribe",
* "controller"=SubscribeToConso::class,
* }
* },
* itemOperations={
* "get",
* },
* )
*/
class ExternalSubscriptionRequest
{
// ...
/**
* @var File the file this request was made for
*
* @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="external_subscription_requests")
* @ORM\JoinColumn(referencedColumnName="id", nullable=false)
*
* @Groups({
* "external_subscription_request:collection:read",
* "external_subscription_request:item:read"
* })
*/
public $file;
// ...
}
你可能失踪了,你可能失踪了。