steffenbrem / json-api-bundle
使用JMS Serializer (FOSRestBundle) 集成JSON API与Symfony2。
0.2
2017-02-08 14:03 UTC
Requires
- php: >=5.3.9
- jms/serializer-bundle: ~0.13|~1.0
- pagerfanta/pagerfanta: ~1.0
- symfony/framework-bundle: ~2.3|~3.0
- willdurand/hateoas: >2.0
Requires (Dev)
- phpspec/phpspec: ~2.4
- symfony/expression-language: ~2.4
- twig/twig: ~1.12
This package is not auto-updated.
Last update: 2024-09-14 17:26:48 UTC
README
将JSON API与Symfony 2 (FOSRestBundle) 集成
请注意,这仍然是一个工作版本(WIP),不应在生产环境中使用!
用法
即将推出
如果您想尝试这个实现,只需在您的 AppKernel 中启用此扩展包,一切应该都能直接工作。尝试序列化一些注解过的PHP类并查看结果!
配置参考
mango_json_api: show_version_info: true # default base_uri: /api # default
注解
@Resource
这将定义您的类为JSON-API资源,并且您可以可选地设置其类型名称。
此注解可以定义在类上。
use Mango\Bundle\JsonApiBundle\Configuration\Annotation as JsonApi; /** * @JsonApi\Resource(type="posts", showLinkSelf=true) */ class Post { // ... }
@Id (可选,默认为 id)
这将定义用于资源 id 的属性。对于同一类型的每个资源,它需要是唯一的。
此注解可以定义在属性上。
use Mango\Bundle\JsonApiBundle\Configuration\Annotation as JsonApi; /** * @JsonApi\Resource(type="posts") */ class Post { /** * @JsonApi\Id */ protected $uuid; }
@Relationship
这将定义一个可以是一对多(oneToMany)或多对一(manyToOne)的关系。您可以可选地设置 includeByDefault 以包含(加载)与主资源的关系。
此注解可以定义在属性上。
use Mango\Bundle\JsonApiBundle\Configuration\Annotation as JsonApi; /** * @JsonApi\Resource(type="posts") */ class Post { // .. /** * @JsonApi\Relationship(includeByDefault=true, showLinkSelf=false, showLinkRelated=false) */ protected $comments; }
配置参考
# app/config/config.yml mango_json_api: show_version_info: true
示例响应
GET /api/channels
{
"jsonapi": {
"version": "1.0"
},
"meta": {
"page": 1,
"limit": 10,
"pages": 1,
"total": 4
},
"data": [
{
"type": "channels",
"id": 5,
"attributes": {
"code": "WEB-UK",
"name": "UK Webstore",
"description": null,
"url": "localhost",
"color": "Blue",
"enabled": true,
"created-at": "2015-07-16T12:11:50+0000",
"updated-at": "2015-07-16T12:11:50+0000",
"locales": [],
"currencies": [],
"payment-methods": [],
"shipping-methods": [],
"taxonomies": []
},
"relationships": {
"workspace": {
"data": {
"type": "workspaces",
"id": 18
}
}
}
},
{
"type": "channels",
"id": 6,
"attributes": {
"code": "WEB-NL",
"name": "Dutch Webstore",
"description": null,
"url": null,
"color": "Orange",
"enabled": true,
"created-at": "2015-07-16T12:11:50+0000",
"updated-at": "2015-07-16T12:11:50+0000",
"locales": [],
"currencies": [],
"payment-methods": [],
"shipping-methods": [],
"taxonomies": []
},
"relationships": {
"workspace": {
"data": {
"type": "workspaces",
"id": 18
}
}
}
},
{
"type": "channels",
"id": 7,
"attributes": {
"code": "WEB-US",
"name": "United States Webstore",
"description": null,
"url": null,
"color": "Orange",
"enabled": true,
"created-at": "2015-07-16T12:11:50+0000",
"updated-at": "2015-07-16T12:11:50+0000",
"locales": [],
"currencies": [],
"payment-methods": [],
"shipping-methods": [],
"taxonomies": []
},
"relationships": {
"workspace": {
"data": {
"type": "workspaces",
"id": 18
}
}
}
},
{
"type": "channels",
"id": 8,
"attributes": {
"code": "MOBILE",
"name": "Mobile Store",
"description": null,
"url": null,
"color": "Orange",
"enabled": true,
"created-at": "2015-07-16T12:11:50+0000",
"updated-at": "2015-07-16T12:11:50+0000",
"locales": [],
"currencies": [],
"payment-methods": [],
"shipping-methods": [],
"taxonomies": []
},
"relationships": {
"workspace": {
"data": {
"type": "workspaces",
"id": 18
}
}
}
}
],
"included": [
{
"type": "workspaces",
"id": 18,
"attributes": {
"name": "First Workspace"
},
"relationships": {
"channels": {
"links": {
"related": "/workspaces/18/channels"
}
}
}
}
]
}