定制 laravel-json-api/neomerx-json-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laravel-json-api/neomerx-json-api

Composer 安装命令:

composer require laravel-json-api/neomerx-json-api

包简介

Framework agnostic JSON API (jsonapi.org) implementation

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage License

Status

This is a maintained fork of neomerx/json-api. It is maintained for use by Laravel JSON:API.

Although we use this package for Laravel JSON:API, it remains framework-agnostic. We have no plans to change that. So you can continue to use it in any PHP application.

Description

JSON API logo

A good API is one of most effective ways to improve the experience for your clients. Standardized approaches for data formats and communication protocols increase productivity and make integration between applications smooth.

This framework agnostic package implements JSON API specification version v1.1 and helps focusing on core application functionality rather than on protocol implementation. It supports document structure, errors, data fetching as described in JSON API Format and covers parsing and checking HTTP request parameters and headers. For instance it helps to correctly respond with Unsupported Media Type (HTTP code 415) and Not Acceptable (HTTP code 406) to invalid requests. You don't need to manually validate all input parameters on every request. You can configure what parameters are supported by your services and this package will check incoming requests automatically. It greatly simplifies API development and fully support specification. In particular

  • Resource attributes and relationships
  • Polymorphic resource data and relationships
  • Compound documents with inclusion of related resources (circular resource references supported)
  • Meta information for document, resources, errors, relationship and link objects
  • Profiles
  • Parsing HTTP Accept and Content-Type headers in accordance with RFC 7231
  • Parsing HTTP query parameters (e.g. pagination, sorting and etc)
  • Sparse fieldsets and customized included paths
  • Errors

High code quality and 100% test coverage with 150+ tests. Production ready.

To find out more, please check out the Wiki and Sample App.

“I'm loving how easy it makes it to quickly implement an api”

Jeremy Cloutier

Full-stack Integration

This package is framework agnostic and if you are looking for practical usage sample you might be interested in Quick start JSON API server application Limoncello App.

The server supports

  • CRUD operations for a few sample data models and Users.
  • Cross-origin requests (CORS) to API server.
  • Authentication (Bearer token) and authorizations for CRUD operations.
  • Support for such JSON API features as resource inclusion, pagination and etc.

Demo app screen-shot

Sample usage

Assuming you've got an $author of type \Author you can encode it to JSON API as simple as this

$encoder = Encoder::instance([
        Author::class => AuthorSchema::class,
    ])
    ->withUrlPrefix('http://example.com/api/v1')
    ->withEncodeOptions(JSON_PRETTY_PRINT);

echo $encoder->encodeData($author) . PHP_EOL;

will output

{
    "data" : {
        "type"       : "people",
        "id"         : "123",
        "attributes" : {
            "first-name": "John",
            "last-name": "Doe"
        },
        "relationships" : {
            "comments" : {
                "links": {
                    "related" : "http://example.com/api/v1/people/123/comments"
                }
            }
        },
        "links" : {
            "self" : "http://example.com/api/v1/people/123"
        }
    }
}

The AuthorSchema provides information about resource's attributes and might look like

class AuthorSchema extends BaseSchema
{
    public function getType(): string
    {
        return 'people';
    }

    public function getId($author): ?string
    {
        return $author->authorId;
    }

    public function getAttributes($author, ContextInterface $context): iterable
    {
        return [
            'first-name' => $author->firstName,
            'last-name'  => $author->lastName,
        ];
    }

    public function getRelationships($author, ContextInterface $context): iterable
    {
        return [
            'comments' => [
                self::RELATIONSHIP_LINKS_SELF    => false,
                self::RELATIONSHIP_LINKS_RELATED => true,

                // Data include supported as well as other cool features
                // self::RELATIONSHIP_DATA => $author->comments,
            ],
        ];
    }
}

Parameter http://example.com/api/v1 is a URL prefix that will be applied to all encoded links unless they have a flag set telling not to add any prefixes.

Parameter JSON_PRETTY_PRINT is a PHP predefined JSON constant.

A sample program with encoding of multiple, nested, filtered objects and more is here.

For more advanced usage please check out the Wiki.

Versions

Current version is 4.x (PHP 7.1+) for older PHP (PHP 5.5 - 7.0, HHVM) please use version 1.x.

Questions?

Do not hesitate to check issues or post a new one.

Need help?

Are you planning to add JSON API and need help? We'd love to talk to you sales@neomerx.com.

Contributing

If you have spotted any specification changes that are not reflected in this package please post an issue. Pull requests for documentation and code improvements are welcome.

There are 2 ways to send pull requests

  • small pull requests should be sent to develop branch as 1 commit
  • for bigger pull requests (e.g. new features) it's recommended to create an issue requesting a new branch for that feature. When a new branch named feature/issueXX is created (where XX is the issue number) you should post pull requests to this branch. When the feature is completed the branch will be squashed and merged to develop and then to master branches.

License

Apache License (Version 2.0). Please see License File for more information.

laravel-json-api/neomerx-json-api 适用场景与选型建议

laravel-json-api/neomerx-json-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.97M 次下载、GitHub Stars 达 9, 最近一次更新时间为 2021 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「api」 「jsonapi」 「JSON-API」 「jsonapi.org」 「neomerx」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laravel-json-api/neomerx-json-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laravel-json-api/neomerx-json-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 laravel-json-api/neomerx-json-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.97M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 25
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 66
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2021-02-27