codeigniter-restserver icon indicating copy to clipboard operation
codeigniter-restserver copied to clipboard

Fetch by id

Open xckon opened this issue 12 years ago • 6 comments

When trying to fetch an Object (using Backbone) this call http://xxxxxxx.com/api/usuarios/439 produces {"status":false,"error":"Unknown method."}

Is it possible the server does not supports GET with ID?

xckon avatar Sep 18 '14 18:09 xckon

Hello, the library supports that easily. Could you please post your controller and routes?

chriskacerguis avatar Sep 18 '14 18:09 chriskacerguis

Oh, i see. I need to define a route like this $route['api/usuarios/(:any)'] = 'api/usuarios/index_get/$1 to make it work right? Sorry, i thought the library was doing it on background. Thanks! =)

xckon avatar Sep 18 '14 18:09 xckon

Just tried, but didn't work: // Routes $route['api/usuarios/(:any)'] = 'api/usuarios/index_get/$1';

// Controller /application/controllers/api/usuarios.php class Usuarios extends REST_Controller { public function index_get($id) { [....] } }

// Browser URL: http://local.xxxxx.com/api/usuarios/439 // Response: {"status":false,"error":"Unknown method."}

xckon avatar Sep 18 '14 19:09 xckon

Your route would be something like this:

$route['api/usuarios/(:any)'] = 'api/usuarios/$1';

Since I can't see your whole controller, I can't totally speak to your code.

chriskacerguis avatar Sep 18 '14 19:09 chriskacerguis

I made it simpler, here is my controller:

// Controller: /application/controllers/api/usuarios.php require_once APPPATH . 'libraries/REST_Controller.php'; class Usuarios extends REST_Controller { public function index_get($id = null) { var_dump($id);exit; } }

// Routes $route['usuarios/(:any)'] = 'usuarios/$1';

// URL http://local.xxxxx.com/usuarios/439

// Browser Output {"status":false,"error":"Unknown method."}

Just updated the library thinking the code was outdated, but keeps doing the same thing =( Any Idea?

xckon avatar Sep 18 '14 19:09 xckon

I solved it. The route was wrong, it should be: $route['usuarios/(:any)'] = 'usuarios/index/$1'; Because index is the "resource". Thanks @chriskacerguis ! =)

xckon avatar Sep 18 '14 20:09 xckon