0

I am using codeigniter to create a restapi and I am having some problems with routes.

Here is the equation:

I have to navigate /users/ to index function,

I have to navigate /users/{MongoId} to /users/show/{MongoId},

I have to navigate /users/function to /users/function.

And here, my routes:

$route['api/users/do_login'] = "api/users/do_login";
$route['api/users/(.*)'] = "api/users/show/$1";

When I remove the (.*) routing (or both of them), my do_login function works successfully. But not my api-index function because Codeigniter takes MongoId as function name and fails.

When I write it back (or both), my index function works successfully, but my login doesn't. Because it tries to send function name to show function as parameter.

Can you please help me to fix that?

Hilmi Erdem KEREN
  • 1,949
  • 20
  • 29

2 Answers2

1

Reverse the order of routes, CodeIgniters routes are prioritized.

You seek this structure:

$route['api/users/(:any)'] = "api/users/show/$1";
$route['api/users/do_login'] = "api/users/do_login";

also use (:any) instead of (.*) they are the same.

CodeIgniter Routing

Kyslik
  • 8,217
  • 5
  • 54
  • 87
  • Oh I tried all of these after asking the question. But the problem seems to be bigger now. I use codeigniter-restserver of philsturgeon and that doesn't save me. I think he used remapping in his REST_Controller and my routes always return 404 (No method). Do you have any idea? – Hilmi Erdem KEREN Jun 10 '13 at 00:52
  • Try remapping in your own controller "...because Codeigniter takes MongoId as function name and fails.", or that is out of question too? – Kyslik Jun 10 '13 at 01:07
  • I now double checked it. When I use the routes you wrote and request some output from his class, It says the method called is named "show_post". It made me think that the :any blocks other one. Anyway, Thanks. – Hilmi Erdem KEREN Jun 10 '13 at 01:21
  • also if its ID use `(:num)` instead. I assume it is still not working as expected. I dug in to RESTserver code and found `_remap()`. What is the url of request? you might change routes to `'api/users/(:any)/(:any)'` or something like that... – Kyslik Jun 10 '13 at 01:27
  • (:num) doesn't work. It don't think that it is hexadecimal is numeric. – Hilmi Erdem KEREN Jun 10 '13 at 01:37
0

Here, the working routes.

$route['api/users/do_login/(:any)'] = "api/users/do_login/$1";
$route['api/users/(:any)'] = "api/users/show/$1";
Hilmi Erdem KEREN
  • 1,949
  • 20
  • 29