简单的PHP REST框架
0.1.0
2018-08-03 20:34 UTC
Requires
- php: ^5.4.0 || ^7.0
This package is not auto-updated.
Last update: 2024-10-02 03:38:29 UTC
README
内容
- 安装
- 设置
- 示例
- 技巧
- 许可证
安装
使用Composer安装RIP:composer require racler-productions/rip
设置
首先,你需要初始化Rip类$rip = new Rip();
然后你可以访问你想要的资源方法。确保你已经设置了一个htaccess文件,该文件将请求转发到应用程序。例如
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
示例
GET
$rip->get("/user/:name/profile/picture", array("name"), $authFilter, function($parameters){
echo 'Welcome ' . $parameters["name"];
});
路径参数以冒号:
标记。
POST
$rip->post("/user/profile/picture", array("imageUrl", "userId"), $authFilter, functions($parameters){
//Handle the request
});
PUT
$rip->put("/user/profile/picture", array("imageUrl", "userId"), $authFilter, functions($parameters){
//Handle the request
});
DELETE
$rip->put("/user/profile/picture", array("userId"), $authFilter, functions($parameters){
//Handle the request
});
过滤器
过滤器是在实际函数执行之前被调用的函数。一个简单的身份验证过滤器可能如下所示
$authFilter = function(){
$token = //get token from header
if($this->tokenIsVald($token)){
return $token
}else{
//Handle invalid token
}
};
技巧
- 经常使用的资源最好放在应用程序的开头。
- 使用前不要有其他输出。
- 在完成每个RIP函数后,防止进一步执行。
许可证
版权所有 2018 Racler Productions
MIT,有关详细信息请参阅LICENSE