jacob-roth/phapi 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

jacob-roth/phapi

Composer 安装命令:

composer require jacob-roth/phapi

包简介

A PHP-based Rest API Framework

README 文档

README

A lightweight PHP-based Rest API Framework

Written for PHP 7.4 and 8.0^

How To Use

Adding Phapi to your project

  • Install

    composer require jacob-roth/phapi
    
  • index.php

    $Routes = new \Phapi\Routes();
    $Routes->Add(new \Phapi\Route(
      "Default",
      "/api/v1/{controller}/{action}"
    ));
    
    $Startup = new \Phapi\Startup($Routes);
    $Startup->Run();
  • .htaccess

    <IfModule mod_rewrite.c>
      Options +FollowSymLinks
      RewriteEngine On
      RewriteRule !index.php index.php
    </IfModule>

Adding API Endpoint

  • Create a new PHP class ending with "Controller" that extends Phapi\Controller (or use an existing Controller class)

    class DefaultController extends \Phapi\Controller
  • Add a public method to act as the new endpoint

    • This method can be called anything you want, or it can be named as an HTTP request method (this will allow you to call the endpoint without having to specify the method in the url)

    • Inside this method, you'll want to specify the HTTP request method (Get, Put, etc.). Although not strictly necessary, it is good practice.

      // This endpoint can be called like
      // PUT /Default/IdPut/2
      public function IdPut(int $id)
      {
        $this->HttpPut();
        return [
          "id" => $id
        ];
      }
      
      // This endpoint can be called like
      // GET /Default/2
      public function GET(int $id)
      {
        $this->HttpGet();
        return "something";
      }
  • Add a Route in index.php that fits your new endpoint (note that a previously existing Route may already work, so adding a new Route may not be necessary)

    • A Route maps a request to an endpoint

    • When adding a Route, you can specify variables in the url like {id}. Two of these variables cannot be used: controller and action which specify the Controller class and method to use. Route variables may be made optional by pre-pending them with a "?", like {?id}

    • You can specify which HTTP request methods are allowed in the route

    • In mapping the route to an endpoint, you can specify the namespace, class, and even the method

    • This is an overview of how to use Routes. For more details, see the Class Specifications > Route section

Class Specifications

  • Controller

    • Specify which HTTP request method is allowed. Use only one for each endpoint. Here are some examples:

      $this->HttpGet();
      $this->HttpPut();
      $this->HttpPost();
      $this->HttpDelete();
    • Change which HTTP response code is returned

      $this->SetResponseCode(\Phapi\HttpCode::Created);
  • Route

    • You will only ever need to initialize Route objects in your index.php. Because this object can be complex, I'm going to go into the construct parameters one at a time

    • The construct method is as follows:

      public function __construct(
        string $Name,
        string $Path,
        ?array $HttpMethods = null,
        string $Namespace = "",
        ?string $DefaultController = null,
        ?string $DefaultAction = null
      )
      • Name is not used except by the user to keep track of how each Route is used. You can enter any string value here.

      • Path is a pattern used to match the path in a request url.

        • For example, if your path was /v1/{controller}/{action} then each of the following would be true

          • /v1/default/getValues would map to the method DefaultController->getValues().

          • /v1/user/login would map to UserController->login()

        • And each of these would not map and instead throw a 404

          • /user/login

          • /default/getValues

          • /v1/user/login/somethingElse

        • An important feature of the Path is variables. Path variables are used to pass data from the request url to the method call.

          • Specifically, the string used to define the path variable needs to be the same string used as the argument of the endpoint method

          • For example, given the method

            public function IdGet(int $id)

            a path must include {id} in order to call the endpoint properly. Such a path can look like this: /api/v1/{controller}/{action}/{id}

          • As you might be able to tell, there are special path variables. Namely, "controller" and "action". You may use these where and when you see fit for your application, however, naming a method argument controller will result in incorrect dat getting passed to your endpoint. I strongly discourage such a practice in a production environment

          • You can also make path variables optional. Simply add a "?" in the path variable string, like {?id} and ensure that the method allows for a null value to be passed to that argument. For example:

            public function IdGet(?int $id)

            using optional variables allow you to perform a mapping where /v1/default/idGet/123 and /v1/default/idGet/ both map to DefaultController->IdGet($id) where the latter simply passes null as $id

      • HttpMethods allows you to limit the Http request methods used in the Route.

        • If you want to limit the Route /v1/{controller}/{action} to only GET and POST requests, you'd use

          [\Phapi\HttpMethod::Get, \Phapi\HttpMethod::Post]
        • The default value, null, behaves the same as

          \Phapi\HttpMethod::All
      • Namespace allows you to map requests to Controllers that are in the given namespace. If your Controller classes are not in a namespace, this is to be left blank

        • For example, given the following Route

          $Routes->Add(new \Phapi\Route(
            "Default",
            "/api/v1/{controller}/{action}",
            \Phapi\HttpMethod::All,
            "V1"
          ));

          an input of /v1/user/login would map to V1\UserController->login()

      • DefaultController and DefaultAction are useful when the Controller class and endpoint method are optional or otherwise not specified by the request url.

        • For example, the following Route

          $Routes->Add(new \Phapi\Route(
            "Calling Methods with Special URLs",
            "/{id}",
            null,
            "V2",
            "Special",
            "DoingSomethingSpecial"
          ));

          will map to V2\SpecialController->DoingSomethingSpecial($id)

  • ApiException

    • Can be thrown where you encounter an error and Phapi will handle the exception

    • You can specify an Http response code and a message string if you want to return something to the caller

    • Example:

      throw new \Phapi\ApiException(\Phapi\HttpCode::BadRequest, "Invalid Input Data");
  • HttpMethod

    • Collections of constants representing HTTP Request Methods

      \Phapi\HttpMethod::Get
      \Phapi\HttpMethod::Head
      \Phapi\HttpMethod::Post
      \Phapi\HttpMethod::Put
      \Phapi\HttpMethod::Delete
      \Phapi\HttpMethod::Connect
      \Phapi\HttpMethod::Options
      \Phapi\HttpMethod::Trace
      \Phapi\HttpMethod::Patch
      \Phapi\HttpMethod::All
  • HttpCode

    • Collection of constants representing HTTP Response Codes

    • I won't list them all here because there are a lot, but here are some examples:

      \Phapi\HttpCode::Ok          // 200
      \Phapi\HttpCode::Created     // 201
      \Phapi\HttpCode::NotFound    // 404

Passing Data to Endpoints

There are two ways to pass data from a request to an endpoint: by request data, and request url

  • Data Passed in Request Data

    • If you want to pass request data to an endpoint, simply add an argument with any name and any PHP object type. This could be "object" or some custom object.

    • Example:

      public function EchoId(RequestObject $req)
      {
        $this->HttpPost();
        return [
          "id" => $req->id
        ];
      }
  • Data Passed in Request Url

    • Data passed in the url is handled by path variables. For details on these, see Class Specifications > Route > Path

Version Limitations With Intent to Upgrade

  • Currently Input and Output data can only be JSON. In a future version I intend to add a way to override the I/O format based on the "Content-Type" and "Accept" HTTP headers

  • Speaking of HTTP headers, there's a lot of room for enhancements here. Currently, Phapi uses exactly zero of these headers when handling the data. Headers like Content-type, Accept, Accept-Encoding, Authorization, Cookie, and all things CORS could have a use in Phapi. There are not plans to add anything except Content-type and Accept right now, but the consideration is there

jacob-roth/phapi 适用场景与选型建议

jacob-roth/phapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 jacob-roth/phapi 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-16