如何组织Luracast Restler类来创建相关的路由端点?



我正在使用Luracast Restler API框架,我想知道如何组织类结构来创建这种风格的路由。

webroot/user/:id/
webroot/user/:id/profile 
webroot/user/:id/tweets 

可以使用GET、PUT、POST、DELETE:

Class user(){
 function get($id){
   Do something when GET/webroot/user/:id/ 
 }
 function put($data){
   Do something when PUT/webroot/user/:data/ 
 }
}
Class profile(){
 function get($id){
   Do something when GET/webroot/user/:id/profile 
 }
}
Class tweets(){
 function get($id){
  Do something when GET/webroot/user/:id/tweets
 }
}

提前感谢!

您需要使用PHP文档注释使用自定义url路由。以下是针对您的用例修改过的示例。

Class User(){
 function get($id){
   //Do something when GET /webroot/user/:id/ 
 }
 function put($data){
   //Do something when PUT /webroot/user/:data/ 
 }
 /**
 * @url GET /:id/profile
 */
 function profile($id){
   //get the profile for specified user
 }
}

您可以使用POST, DELETE, PUT方法来替换配置文件编辑。还要注意,你可以使用上面的语法添加多个路由。

其他CRUD方法允许自定义路由吗?

/**
* @url POST /create
*/      
protected function post($request_data=NULL) {
    return $this->dp->insert($this->_validate($request_data));
}

正确调用POST

Prepared POST URLRequest '<NSMutableURLRequest http://myserver.com/base/_006_crud/user>'. HTTP Headers: {
    Accept = "application/json";
    "Content-Type" = "application/x-www-form-urlencoded";
}. HTTP Body: ssoid=123&ssotype=facebook&crudID=0&ssoname=mickey.

但是当我从我的代码调用我得到一个404 "Not Found"错误"

response body: {
  "error": {
    "code": 404,
    "message": "Not Found"
  }
}

最新更新