api-platform / api-platform/core
Standalone business model not belonging to collection
- 主要语言
- PHP
- 星标
- 2.6k
- 派生
- 980
- 平均合并
- 2 天 2 小时
- 30 天内合并 PR
- 51
描述
**Description**
It would be convenient to declare a Resource as a simple Item, without attaching it to a collection, and thus without an identifier property to identify it in a non-sense collection in this case.
A business model is not synonym to `a set of collections of items`.
Of course most elements of my business model are collections, but rarely 100%.
Sometimes their is resources that are unique or let's say standalone (don't know if this is the right word to describe it).
**Examples**
Here are 2 examples :
1. If I make an API for my blog. I want to publish a global abstract, with some general informations about the blog itself. My application is a blog, not a collection of blogs, so it has an abstract, not a collection of abstracts :
```
#[ApiResource(
description: 'The blog overview',
inCollection: false, // default to true
itemOperations: [
// GET api.myblog.com/overview
// ==> { 'author': 'cdr', 'created_at': '2010-01-01', 'last_article': 'api.myblog.com/articles/standalone-resource-request' }
'get' => ['path' => '/overview'],
// PATCH api.myblog.com/overview ; so that admins can modify the 'lastArticle' property
'patch' => ['path' => '/overview']
],
routePrefix: '/'
)]
class BlogAbstract
{
// #[ApiProperty(identifier: true)] -- No identifier, this is 'THE' abstract of 'THE' blog
// public string $id = 'TheOnlyOne';
private string $author = '@me';
private \DateTime $createdAt; //= new \DateTime('2010-01-01');
private string $lastArticle = 'I would love to be able to define Item resource that do not stand within a collection';
}
```
2. If I make an e-commerce application, consumers have a prepaid account, not a collection of prepaid accounts :
```
#[ApiResource(
description: 'Your prepaid account',
inCollection: false, // default to true
itemOperations: [
// GET api.my-ecommerce.com/users/{userId}/prepaidAccount
// ==> { 'amount': '1300 CHF' }
'get' => ['path' => '/users/{userId}/prepaidAccount'],
],
routePrefix: '/'
)]
/**
* Note : I really want userId to be before prepaidAccount,
even if I could use the 'userId' as a unique identifier of the prepaidAccount,
because I like to keep consistence with the relations semantic, and with my other uri like this :
GET api.my-ecommerce.com/users/{userId}/orders
GET api.my-ecommerce.com/users/{userId}/orders/{orderId}
GET api.my-ecommerce.com/users/{userId}/prepaidAccount
*/
class PrepaidAccount
{
// #[ApiProperty(identifier: false)] -- No identifier, this is 'THE' prepaidAccount of the user
// public string $id = 'TheOnlyOne';
private float $amount = 1300;
private string $currency = 'CHF';
}
```
3. NB: this concept exists even in the more basic Swagger-OA3 official get started example, so I think it's really a standard and acceptable behaviour : https://petstore.swagger.io/?displayOperationId=false#/store/getInventory
贡献指南
评估
这个 Issue 还没有评估数据。