承接 bornfight/jsonapi-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

bornfight/jsonapi-bundle

Composer 安装命令:

composer require bornfight/jsonapi-bundle

包简介

The jsonapi-bundle is a Symfony bundle. it is the fastest way to generate API based on JsonApi.org using woohoolabs/yin

README 文档

README

Latest Stable Version Build Status License: MIT Total Downloads

JsonApiBundle For Symfony

JsonApiBundle is a Symfony bundle. It is the fastest way to generate API based on JsonApi using woohoolabs/yin Library.

Installing

  1. Install symfony

    composer create-project symfony/skeleton YOUR_PROJECT
    
  2. Install the maker bundle

    composer require symfony/maker-bundle phootwork/collection --dev
    
  3. Install the bundle

    composer require paknahad/jsonapi-bundle
    
  4. Add below line to config/bundles.php

    Paknahad\JsonApiBundle\JsonApiBundle::class => ['all' => true],
    

Usage

  1. Use below command to generate entities one by one:

    bin/console make:entity
    

    for example, Book and Author entity is as follows:

    class Book
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=255)
         */
        private $title;
    
        /**
         * @ORM\Column(type="string", length=20, nullable=true)
         */
        private $isbn;
    
        /**
         * @ORM\ManyToMany(targetEntity="App\Entity\Author", inversedBy="books")
         */
        private $authors;
     
        ... 
    class Author
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=255)
         * @Assert\NotBlank()
         * @Assert\Length(min=3)
         */
        private $name;
    
        /**
         * @ORM\ManyToMany(targetEntity="App\Entity\Book", mappedBy="authors")
         */
        private $books;
     
        ...
  2. Generate CRUD API:

    bin/console make:api
    
  3. You can find the generated "collections" for postman and swagger in the following path and then test the API:

    collection/postman.json
    collection/swagger.yaml
    

Features

Pagination

http://example.com/books?page[number]=5&page[size]=30

Sorting

  • Ascending on name field: http://example.com/books?sort=name
  • Decending on name field: http://example.com/books?sort=-name
  • Multiple fields: http://example.com/books?sort=city,-name
  • Field on a relation: http://example.com/books?sort=author.name

Relationships

http://example.com/books?include=authors

multiple relationships

http://example.com/books?include=authors.phones,publishers

Search

As the JSON API specification does not specify exactly how filtering should work different methods of filtering can be used. Each method is supplied with a Finder service. Each registered Finder will be able to append conditions to the search query. If you register multiple Finders they are all active at the same time. This enables your API to support multiple filtering methods.

Basic Finder.

A basic Finder is included in this library offering simple filtering capabilities:

This request will return all the books that author's name begin with hamid

http://example.com/books?filter[authors.name]=hamid%

Below line has additional condition: books which have "php" in their title.

http://example.com/books?filter[title]=%php%&filter[authors.name]=hamid%

Other Finders

Currently the following Finders are available via other bundles:

Creating a custom Finder

A Finder can be registered via a service tag in the services definition. The tag paknahad.json_api.finder must be added to the service for the Finder to be resigered.

Example:

<service class="Paknahad\JsonApiBundle\Helper\Filter\Finder" id="paknahad_json_api.helper_filter.finder">
    <tag name="paknahad.json_api.finder" />
</service>

Each Finder must implement the Paknahad\JsonApiBundle\Helper\Filter\FinderInterface interface.

Validation

Error on validating associations

{
    "jsonapi": {
        "version": "1.0"
    },
    "errors": [
        {
            "detail": "Invalid value for this relation",
            "source": {
                "pointer": "/data/relationships/authors",
                "parameter": "1"
            }
        }
    ]
}

Validate attributes if you have defined validators on entities.

{
    "jsonapi": {
        "version": "1.0"
    },
    "errors": [
        {
            "detail": "This value is too short. It should have 3 characters or more.",
            "source": {
                "pointer": "/data/attributes/name",
                "parameter": "h"
            }
        }
    ]
}

Error handler

All errors such as:

  • Internal server error (500)
  • Not found (404)
  • Access denied (403)

has responses like this:

{
    "meta": {
        "code": 0,
        "message": "No route found for \"GET /book\"",
        "file": "/var/www/vendor/symfony/http-kernel/EventListener/RouterListener.php",
        "line": 139,
        "trace": [
            {
                "file": "/var/www/vendor/symfony/event-dispatcher/EventDispatcher.php",
                "line": 212,
                "function": "onKernelRequest"
            },
            {
                "file": "/var/www/vendor/symfony/event-dispatcher/EventDispatcher.php",
                "line": 44,
                "function": "doDispatch"
            },
            {
                "file": "/var/www/vendor/symfony/http-kernel/HttpKernel.php",
                "line": 125,
                "function": "dispatch"
            },
            {
                "file": "/var/www/vendor/symfony/http-kernel/HttpKernel.php",
                "line": 66,
                "function": "handleRaw"
            },
            {
                "file": "/var/www/vendor/symfony/http-kernel/Kernel.php",
                "line": 188,
                "function": "handle"
            },
            {
                "file": "/var/www/public/index.php",
                "line": 37,
                "function": "handle"
            }
        ]
    },
    "links": {
        "self": "/book"
    },
    "errors": [
        {
            "status": "404",
            "code": "NO_ROUTE_FOUND_FOR_\"GET_/BOOK\"",
            "title": "No route found for \"GET /book\""
        }
    ]
}

NOTICE: the "meta" field gets filled just on development environment.

bornfight/jsonapi-bundle 适用场景与选型建议

bornfight/jsonapi-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 981 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 07 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 bornfight/jsonapi-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 981
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 7
  • Forks: 26
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-08