blacknell/restapi-service
Composer 安装命令:
composer require blacknell/restapi-service
包简介
Simple class to expose a REST api
README 文档
README
restapi-service maps REST API calls to endpoints in protected methods in your derived class.
In your class methods process according to the verbs and arguments of the http request.
Installation
Install the latest version with
$ composer require blacknell/restapi-service
Basic Usage
- Copy
example/api.phpand derive a class such as inexamples/MyAPI.class.phpinto your web server directory - Configure a
.htaccessfile to rewrite your RESTful call to your class
Web Server configuration
For example, https://yourserver/myapi/v1/daylight/littlehampton/yesterday maps to
https://yourserver/myapi/v1/api.php?request=daylight/littlehampton/yesterday
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule myapi/v1/(.*)$ myapi/v1/api.php?request=$1 [QSA,NC,L]
</IfModule>
Sample code
See example/MyAPI.class.php
to see how https://yourserver/myapi/v1/daylight/littlehampton/yesterday generates the following JSON output
{
"description": "Between sunrise and sunset yesterday",
"sunrise": {
"date": "2019-01-07 08:00:56.000000",
"timezone_type": 3,
"timezone": "Europe\/London"
},
"sunset": {
"date": "2019-01-07 16:15:44.000000",
"timezone_type": 3,
"timezone": "Europe\/London"
}
}
Additional Concepts
Cross-Origin Resource Sharing (CORS)
Additional headers can be added to the constructor of your derived class before calling the parent constructor. For example, to allow a client on a website http://myclient.com to access your API add this header call.
public function __construct($request, \Monolog\Logger $logger = null)
{
header('Access-Control-Allow-Origin: http://myclient.com');
parent::__construct($request, $logger);
}
Authentication
Overide RestAPI::isAuthenticated() to handle authentication and only return true if the request is authorised.
As a basic example, you could enforce a request to include a header such as Authentication-Token: xxx and test this in your derived class.
protected function isAuthenticated()
{
$headers=getallheaders();
if($headers['Authentication-Token'] !== 'xxx') {
return false;
} else {
return parent::isAuthenticated();
}
}
Error Handling
Any endpoint not mapping to a protected function in your derived class results in the following JSON response.
{
"error": "No endpoint",
"code": 404
}
Your derived class should do the same for invalid verbs or arguments. Methods other than GET, POST, PUT or DELETE also result in an error.
Logging
PSR-3 logging is supported via monolog/monolog by passing
an optional Logger object to the API constructor.
blacknell/restapi-service 适用场景与选型建议
blacknell/restapi-service 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 242 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 blacknell/restapi-service 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 blacknell/restapi-service 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 242
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-08