Search code examples
phprestler

Restler get/index with optional value


I'm trying to define a method with Restler so that if I do .../clients/123 then it returns that specific client, and if I leave off the 123 then it returns all clients. I tried this:

/**
 * @param int $id The SQL identifier 
 */
function index($id = NULL) {
}

If I do it like that, then it works with query params: .../clients?id=123

Then when I add {@from path} to the end of that @param so that I do use a query string, it doesn't work with no value specified, but does with with the 123 specifier. What's the right way to do this so that I have it as a path instead of a query parameter?


Solution

  • as a best practise reserve index for getting a collection and use get for specific id as shown below

    function index(){
        return Client::all()->toArray();
    }
    
    function get($id){
        return Client::find($id)->toArray();
    }