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
publicmethod 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:controllerandactionwhich 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 > Routesection
-
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 )
-
Nameis not used except by the user to keep track of how each Route is used. You can enter any string value here. -
Pathis 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/getValueswould map to the methodDefaultController->getValues(). -
/v1/user/loginwould map toUserController->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 argumentcontrollerwill 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 anullvalue 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/123and/v1/default/idGet/both map toDefaultController->IdGet($id)where the latter simply passesnullas$id
-
-
-
HttpMethodsallows 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
-
-
Namespaceallows 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/loginwould map toV1\UserController->login()
-
-
DefaultControllerandDefaultActionare 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
- Data passed in the url is handled by path variables. For details on these, see
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jacob-roth/phapi 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
High-performance, opinionated PHP 8 web framework with O(1) routing, parallel async MySQL, type-safe asset bridge, and React-island frontend
BlockCypher's PHP SDK for REST API
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-16