PHP Restler支持包含在其他URL中的api URL

PHP Restler支持包含在其他URL中的api URL,php,api,rest,restler,Php,Api,Rest,Restler,。。想不出一个足够描述性的标题。我要问的是我该怎么做 我需要以下两个API调用 GET /api/users/2/duels - returns all of the duels for user 2 GET /api/users/2 - returns the profile for user 2 因为PHP不支持方法重载,所以我不清楚如何使其工作 目前我有这个功能 function get($id, $action){ //returns data based on a

。。想不出一个足够描述性的标题。我要问的是我该怎么做

我需要以下两个API调用

 GET /api/users/2/duels - returns all of the duels for user 2 
 GET /api/users/2 - returns the profile for user 2
因为PHP不支持方法重载,所以我不清楚如何使其工作

目前我有这个功能

 function get($id, $action){
      //returns data based on action and id
 }
我不能就这么做

 function get($id){
      //returns profile based on id
 } 
因为上述原因


非常感谢您的帮助

您可以使用@url phpdoc decorator告诉restler任何与直接类->方法映射不匹配的特殊调用方案

/**
 * @url GET /api/users/:userId/duels
 */
public function getDuels($userId)
{

}

。。应该可以工作。

一种方法是使用条件块在同一个函数中处理这两种情况,如下所示

function get($id, $action=null){
    if(is_null($action)){
        //handle it as just $id case
    }else{
        //handle it as $id and $action case
    }
}
如果您正在运行restler 3及更高版本,则必须禁用智能路由

/**
* @smart-auto-routing false
*/
function get($id, $action=null){
    if(is_null($action)){
        //handle it as just $id case
    }else{
        //handle it as $id and $action case
    }
}
另一种方法是使用多个函数,因为索引也映射到根,所以您只有很少的选项,您可以将函数命名为get、index、getIndex

function get($id, $action){
    //returns data based on action and id
}
function index($id){
    //returns profile based on id
}
如果您正在使用Restler 2或关闭智能路由,则函数的顺序对您很重要

如果函数名的选项已用尽,可以按照@FiskDisk的建议使用@url映射,但路由应仅包括从方法级别,因为类路由始终在前面加上前缀,除非使用
$r->addAPIClass('MyClass','')将其关闭

function get($id){
    //returns data based on action and id
}

/**
 * @url GET :id/duels
 */
function duels($id)
{

}