json-api-php / json-api
作为一组PHP类的JSON API规范(jsonapi.org)
3.0.0
2022-07-27 01:10 UTC
Requires
- php: >=8.1
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.9
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-08-27 05:46:14 UTC
README
此库的目标是确保生成的JSON API文档的严格有效性。
JSON
{ "data": { "type": "articles", "id": "1", "attributes": { "title": "Rails is Omakase" }, "relationships": { "author": { "data": { "type": "people", "id": "9" }, "links": { "self": "/articles/1/relationships/author", "related": "/articles/1/author" } } } } }
PHP
<?php use JsonApiPhp\JsonApi\Attribute; use JsonApiPhp\JsonApi\DataDocument; use JsonApiPhp\JsonApi\Link\RelatedLink; use JsonApiPhp\JsonApi\Link\SelfLink; use JsonApiPhp\JsonApi\ResourceIdentifier; use JsonApiPhp\JsonApi\ResourceObject; use JsonApiPhp\JsonApi\ToOne; echo json_encode( new DataDocument( new ResourceObject( 'articles', '1', new Attribute('title', 'Rails is Omakase'), new ToOne( 'author', new ResourceIdentifier('author', '9'), new SelfLink('/articles/1/relationships/author'), new RelatedLink('/articles/1/author') ) ) ), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
安装
composer require json-api-php/json-api
文档
首先,查看示例。所有示例都是可运行的。
库API和使用案例在一系列全面的测试中表达。
- 数据文档(包含主要数据)
- 复合文档
- 错误文档
- 元文档(既不包含数据也不包含错误)
- 分页
- 链接对象
- JSON API对象
- 元对象