php-platform/restful
Composer 安装命令:
composer require php-platform/restful
包简介
README 文档
README
This packages provides platform for writing RESTFul APIs in PHP
Introduction
RESTful APIs are modern way of designing an web application , this approach provides the UI Dresigners complete freedom of their design and improvement
RESTFul APIs also give a way to secure the web resources in more elegant way
This package provides a platform for creating such RESTFul APIs in PHP
Features
- Annotated APIs
- can be used with any frameworks
Usage
- copy
resources/.htaccessandresources/index.phpto the root of the composer package - enable apache
rewritemodule - add
AllowOveride Allto composer root directory in apache's configuration - Service Class must implement
`PhpPlatform\RESTFul\RESTService` - annotate service classes and methods with
`@Path` to specify the path to which the the perticular class::method provides the service - configure routes as mentioned in the Configuration section below
- All service methods must return
`PhpPlatform\RESTFul\HTTPResponse`
Annotations
@Path
Can be applied on service class or method , this denotes url path to reach that service-method
@GET @POST @PUT @PATCH @HEAD @DELETE
Can be applied only on service method , denotes what http verb this method is capable of serving.
A service method can have more than on of these Annotations
If a service method has @Path annotation and has no HTTP methods , @GET is applied by default
@Consumes
Cab be applied only on service method, specifies the data type of the request body
deserializers use this annnotation to deserialize the http request body into a php data. refer deserilizers section to configure multiple deserializers
@ReCaptcha
Enables service method to have recaptcha authenticated.
Considered when recaptcha.enable configuration is set to true
NOTE :
service request should send the recaptcha response as http header Php-Platform-Recaptcha-Response
@CORS.force
Can be applied on service class or method
Forces Origin Header to be present in Request
This Annotation forces CORS on a service, see CORS section of this document for more information
Configuration
This section explains the configuration for this package which can be configured using config.xml
serializers
Differrent type of data needs to be serialized in differrent formats based on the Accept Header in the request
So differrent serializing implementations can be configured as follows
"serializers":{
"array":{
"application/json":"PhpPlatform\\RESTFul\\Serialization\\JsonToArraySerialization"
},
"SimpleXMLElement":{
"application/xml":"PhpPlatform\\RESTFul\\Serialization\\XmlToSimpleXMLElementSerialization"
}
},
Serializer class must implement `PhpPlatform\RESTFul\Serialization\Serialize` interface
deserializers
The data in the http request must be converted into a php represenation So differrent deserializing implementations can be configured as follows
"deserializers":{
"application/json":{
"array":"PhpPlatform\\RESTFul\\Serialization\\JsonToArraySerialization"
},
"application/xml":{
"SimpleXMLElement":"PhpPlatform\\RESTFul\\Serialization\\XmlToSimpleXMLElementSerialization"
}
},
Deserializer class must implement `PhpPlatform\RESTFul\Serialization\Deserialize` interface
The PHP type to which the data should be converted needs to be specified at an annotation for the service as follows
/**
* @Consumes array
* @Path my-service
*/
function myService(){}
routes
routes is the static map of url pattern to service class and methods.
routes can be updated manually or generated based on the annotations by running
$ ./vendor/bin/build-restful
routes is organized as a tree , where each node contains the class and method name of the service available for that url path
web services for these url patterns will be configured as follows
`GET /user/all :- MyService\User::getAllUsers``POST /user/create :- MyService\User::createUser``GET /user/{id} :- MyService\User::getUser`
"routes" : {
"children" : {
"user" : {
"children" : {
"all" : {
"methods" : {
"GET" : {
"class" : "MyService\\User",
"method" : "getAllUsers"
}
}
},
"create" : {
"methods" : {
"POST" : {
"class" : "MyService\\User",
"method" : "createUser"
}
}
},
"*" : {
"methods" : {
"GET" : {
"class" : "MyService\\User",
"method" : "getUser"
}
}
}
}
}
}
}
`NOTE :`
`The parameters in the path are represented as * in the config , in the above example {id} is represented as *`
`Name of the params does not map to the name of the service method arguments , but they map to the position`
recaptcha
recaptcha configurations enable services to require reCaptcha authentication
"recaptcha":{
"enable":true,
"secret":""
}
set enable to true to enable services to have recaptcha
set the value of secret provided from google recaptcha
CORS
CORS configurations enables CORS (Cross Origin Resource Sharing) authentication
"CORS":{
"AllowOrigins":[
],
"AllowMethods":[
],
"AllowHeaders":[
],
"AllowCredentials":false,
"MaxAge":1000
}
Details of these can be found in https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
CORS configurations can be set specific for a Service using Annotations.
For example to allow a https://specific.example.com to a specific service
/**
* ...
* @CORS.AllowOrigins https://specific.example.com
*/
function mySpecificService(){}
Access-Control-* Headers
Access-Control-Allow-Origin This header is set to
originheader in the request only if thatoriginis listed inCORS.AllowOriginsconfigurationAccess-Control-Allow-Methods Comma seperated Methods configured in
CORS.AllowMethodsAccess-Control-Allow-Headers Comma seperated Headers configured in
CORS.AllowHeadersAccess-Control-Allow-Credentials True/False configured in
CORS.AllowCredentialsAccess-Control-Max-Age number of seconds configured in
CORS.MaxAgeAccess-Control-Exposed-Headers Comma separated names of headers explicitely set in HTTPResponse object from the service
php-platform/restful 适用场景与选型建议
php-platform/restful 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.1k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「restful」 「web services」 「rest services」 「php restful」 「REST APIs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-platform/restful 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-platform/restful 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-platform/restful 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
A PHP client for the Salesforce SOAP API
Model of a web-based resource
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
Retrieve a WebResourceInterface instance over HTTP
This is a restful api to onesignal.
统计信息
- 总下载量: 1.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2017-05-24