0
votes

I am actually following the tutorial for REST API in Codeigniter http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814

It works fine for simple use cases.

But I would to set custom routes url to have the use of my api more user friendly, I explain...

For example : I want to get the list of students of a dedicated classroom I would like the user to set http:myurl/api/classroom/1/students/

In route config files I set : $route['api/classroom/(:num)/students/'] = 'api/classroom/students/$1';

It does not work because the REST API interprete the (:num) as a method/function. Is there a way to manage this case ?

In another case, it should be nice to have the following use case : for classroom id 1, get data for student with id 2 => http:myurl/api/classroom/1/students/2

Thanks for your help :)

1
Try to add some code from the route file and the classroom controller. If you set a function named 'students' with 1 or 2 params, it doesn't work? Like: classroom.php: function students($id_classroom = null, $id_student = null){ ? Maybe you should delete the api/ from your route assignation part. - JP. Aulet
In fact i managed to handle it, thanks - Ced

1 Answers

0
votes

I managed to handle it, like the example below

In route file :

$route['(api/users/email/(:any)/password/(:any))'] = 'api/users/by_credentials/$2/$3'; // User by id

In my controller, get params like this

public function by_credentials_get($email,$password){
    // ... ex: this->mymodel->testcredentials($email,$password)
}