0

How can I pass URI parameters in the route when using controller function?

For example:

    $app->get('/api/courts/{id}/{date}',
    'C:\xampp\htdocs\backend\App\Controllers\AvailabilityController:
    getCourtAvailability'){
    $id=$request->getAttribute('id');
    $date=$request->getAttribute('date');
    return json_encode();
    }; 

Is the above method correct ??

Ayush28
  • 359
  • 3
  • 18

1 Answers1

3

By default, arguments named in route are passed in function as third argument (an array), so in your AvailabilityControler, they must be a function named getCourtAvailability like this :

getCourtAvailability ($request, $response, $args){
    $id = $args['id']; // because id is set in route
    $date = $args['date']; //because date is set in route
    // your treatement here
}

Hope I help ;)

ekans
  • 1,662
  • 14
  • 25