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

时间:2011-11-12 23:14:00

标签: php api frameworks

我正在使用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
 }
}

提前致谢!

2 个答案:

答案 0 :(得分:2)

您需要使用PHP Doc评论使用自定义网址路由。以下是您的用例的修改示例。

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替换该方法以进行配置文件编辑。另请注意,您可以使用上面的语法添加多个路径。

答案 1 :(得分:1)

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

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

我正确地打电话给

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“未找到”错误“

response body: {
  "error": {
    "code": 404,
    "message": "Not Found"
  }
}
相关问题