0

.. Couldn't think of a descriptive enough title. What I'm asking for is how do I do this?

I want the following 2 API calls

 GET /api/users/2/duels - returns all of the duels for user 2 
 GET /api/users/2 - returns the profile for user 2

Since PHP doesn't support method overloading, it isn't clear to me how to make this work.

Currently I have the function

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

And I can't just make

 function get($id){
      //returns profile based on id
 } 

because of said reasons.

Any help is greatly appreciated!!!

JP.
  • 544
  • 5
  • 20

2 Answers2

0

You can use the @url phpdoc decorator to tell restler of any special invocation schemes that doesn't match the direct class->method mapping.

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

}

.. should probably work.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
0

One approach is handling both cases in same function with a conditional block as shown below

function get($id, $action=null){
    if(is_null($action)){
        //handle it as just $id case
    }else{
        //handle it as $id and $action case
    }
}

if you are running restler 3 and above you have to disable smart routing

/**
* @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
    }
}

Another approach is to have multiple functions since index also maps to root, you have few options, you can name your functions as get, index, getIndex

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

If you are using Restler 2 or turning smart routing off, order of the functions is important to fight ambiguity

If you are running out of options for function names you can use @url mapping as @fiskfisk suggested but the route should only include from method level as the class route is always prepended unless you turn it off using $r->addAPIClass('MyClass','');

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

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

}

HTH

Arul Kumaran
  • 983
  • 7
  • 23