如何为同一实体公开不同的 GET 路由
How to expose different GET routes for the same entity
我使用 Symfony 作为后端和 API-平台。我有一个这样的实体用户:
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
*/
#[ApiResource(
itemOperations: [
'get' => [
'normalization_context' => ['groups' => ['user:read']],
],
],
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
#[Groups(['user:read'])]
private string $email;
/**
* @ORM\Column(type="string", length=255)
*/
#[Groups(['user:read'])]
private string $firstname;
/**
* @ORM\Column(type="string", length=255)
*/
#[Groups(['user:read'])]
private string $lastname;
/**
* @ORM\OneToMany(targetEntity=Project::class, mappedBy="user", orphanRemoval=true)
*/
#[Groups(['user:read'])]
private Collection $foo;
/**
* @ORM\OneToOne(targetEntity=MediaObject::class, cascade={"persist", "remove"})
*/
#[ApiProperty(iri: 'http://schema.org/image')]
#[Groups(['user:read'])]
private ?MediaObject $picture1;
/**
* @ORM\OneToOne(targetEntity=MediaObject::class, cascade={"persist", "remove"})
*/
#[ApiProperty(iri: 'http://schema.org/image')]
#[Groups(['user:read'])]
private ?MediaObject $picture2;
}
目前,我有一条用于某项的 GET 路由:/api/users/{id}
我想为一个项目设置 3 个 GET 路由:
- /api/users/{id}/data 我将在其中包含电子邮件、名字和姓氏
- /api/users/{id}/foo 我应该有 foo
- /api/users/{id}/图片
我会有 picture1 和 picture2
是否可能,如果可能,如何实现?
一切都解释清楚了here。
你必须这样做:
#[ApiResource(
itemOperations: [
'get' => [
'normalization_context' => ['groups' => ['user:read']],
],
'get_data' => [
'method' => 'get',
'path' => '/api/users/{id}/data',
'normalization_context' => [
'user:read:data'
]
],
],
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
// ...
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
#[Groups(['user:read', 'user:read:data'])]
private string $email;
// ...
我使用 Symfony 作为后端和 API-平台。我有一个这样的实体用户:
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
*/
#[ApiResource(
itemOperations: [
'get' => [
'normalization_context' => ['groups' => ['user:read']],
],
],
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
#[Groups(['user:read'])]
private string $email;
/**
* @ORM\Column(type="string", length=255)
*/
#[Groups(['user:read'])]
private string $firstname;
/**
* @ORM\Column(type="string", length=255)
*/
#[Groups(['user:read'])]
private string $lastname;
/**
* @ORM\OneToMany(targetEntity=Project::class, mappedBy="user", orphanRemoval=true)
*/
#[Groups(['user:read'])]
private Collection $foo;
/**
* @ORM\OneToOne(targetEntity=MediaObject::class, cascade={"persist", "remove"})
*/
#[ApiProperty(iri: 'http://schema.org/image')]
#[Groups(['user:read'])]
private ?MediaObject $picture1;
/**
* @ORM\OneToOne(targetEntity=MediaObject::class, cascade={"persist", "remove"})
*/
#[ApiProperty(iri: 'http://schema.org/image')]
#[Groups(['user:read'])]
private ?MediaObject $picture2;
}
目前,我有一条用于某项的 GET 路由:/api/users/{id} 我想为一个项目设置 3 个 GET 路由:
- /api/users/{id}/data 我将在其中包含电子邮件、名字和姓氏
- /api/users/{id}/foo 我应该有 foo
- /api/users/{id}/图片 我会有 picture1 和 picture2
是否可能,如果可能,如何实现?
一切都解释清楚了here。
你必须这样做:
#[ApiResource(
itemOperations: [
'get' => [
'normalization_context' => ['groups' => ['user:read']],
],
'get_data' => [
'method' => 'get',
'path' => '/api/users/{id}/data',
'normalization_context' => [
'user:read:data'
]
],
],
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
// ...
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
#[Groups(['user:read', 'user:read:data'])]
private string $email;
// ...