定制 hq9000/php-rest-router 二次开发

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

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

hq9000/php-rest-router

Composer 安装命令:

composer require hq9000/php-rest-router

包简介

A framework for building REST API structures

README 文档

README

A simple opinionated PHP framework for building API structures.

Motivation

In one of the proprietary projects I was working on, we needed to have a huge and complex REST api surface. Due to its dynamic nature and deep branching, it was considered suboptimal to go the normal "method per action" way.

What was needed is to be able to define a certain meta-structure and have the endpoints be just as a reflection of it.

Main concepts

The main concepts of the framework are the ones of:

  • a structure (a tree of nodes)
  • node traversing

Structure (a tree of nodes)

A Structure is represented by hq9000\PhpRestRouter\Structure class.

image

Structure is a unidirectional, acyclic graph of Nodes.

Each node, except the root one, has exactly one input node.

note: the arrows on the diagram above rather show the direction of the traversing, and not the direction of association.

Also, every node has associated:

  • path processor - something that is responsible for gathering a piece of information relevant to this node
  • path trigger - something that will make node traversing choose this node instead of its sibling.
    • if a node is the only output of its upstream node, the trigger can be omitted

Node traversing

The useful thing a structure makes possible is ability to traverse it.

Traversing is done by calling its public function trace($path, &$dataAccumulator), where:

  • path is a string representing an API endpoint
  • dataAccumulator is a passed-by-reference array that will be used by path processors to put information extracted from the path at each visited node.

The ultimate goal of traversing is to come up with two things:

  • the final node
  • the fully populated accumulator array

the user of the framework may want to associate certain logic with these bits of information, but this is intentionally left out of scope of the framework.

Usage Example

This chapter gives a bit of human-readable explanation to complement reading the source code of CommonTest.php.

In that test, we pretend that the useful thing nodes can do is to hold certain tags. For that, we extend the Node class by DomainNode:

/*
 * Here in test we use a simple subclass of a Node modeling some "Domain" class doing 
 * something actually useful. This one can hold some "tag", in reality,
 * it might, for instance, be able to handle a web request etc.
 */
class DomainNode extends Node
{

    /**
     * @var
     */
    private $tag;

    public function getTag()
    {
        return $this->tag;
    }

    public function setTag($tag)
    {
        $this->tag = $tag;
        return $this;
    }
}

The structure of these nodes is shown on the simplified diagram below:

image

The test itself performs the "tracings" and checks their results, for example that class2/123123 results in the $idNode found as the final one and the data array appropriatelly populated with

{
  "class": "class2",
  "id": 123123
}

hq9000/php-rest-router 适用场景与选型建议

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

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

围绕 hq9000/php-rest-router 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-09-08