sbronsted / librest
基于资源的 REST API
Requires
- php: >=7.1
- ext-json: *
- sbronsted/libcommon: ^3.1
- sbronsted/libutil: ^2.0
Requires (Dev)
- phpunit/phpunit: ^8.5
- ruckusing/ruckusing-migrations: ^1.0
- sbronsted/libdatabase: ^2.0
- sbronsted/libtypes: ^2.1
README
#librest
这个库处理 REST HTTP 请求,URL 被解释为资源请求。所谓的资源是指一个域对象模型,其中那些想要公开的类必须实现 RestEnable。
URL 架构如下
/rest/ClassName[/uid|a_static_method|/uid/a_method][?property=value&...]
示例
GET /rest/Person 将返回所有人员对象
GET /rest/Person/1 将返回 id 为 1 的人员对象
GET /rest/Person/pets 将调用 Person 类的静态方法 'pets' 并返回输出
GET /rest/Person/1/cats 将调用 uid 等于 1 的人员对象的相应方法并返回输出
GET /rest/Person?name=foo 将返回所有名为 'foo' 的人员对象
GET /rest/Person/pets?name=xx 将调用 Person 类的静态方法 'pets' 并传入参数 'xx',然后返回输出
DELETE /rest/Person 将删除所有人员对象
DELETE /rest/Person/1 将删除
POST /rest/Person/pets?property=name,... 将设置所有属性并调用 Person 的静态函数。
POST /rest/Person/1?property=name,... 将设置所有属性并调用 id 为 1 的人员对象的 save 方法
配置
如果 DiContainer 对象配置了名为 restAuthenticator 的对象,那么在执行之前,REST 调用将进行身份验证。
通常你可以在 index.php 中检查 $_SERVER['REQUEST_URI'],如果它以 /rest/ 开头,那么调用:echo Rest::run($_SERVER, $_REQUEST);