Codeigniter 2 Routing with undeterminded number of segments/parameters
-
20-06-2021 - |
Pergunta
I am using Codeigniter 2 and am currently using the 'Routers' config file to set the routes. I am also using IonAuth library. I have a code that does something like:
$route['admin/(login|logout|change_password|forgot_password|reset_password
|activate|deactivate|create_user)'] = "auth/$1";
Now my problem is that, in some of the IonAuth methods, there are none, 1 or 2 parameters. If I try to access the url like:
http://localhost/ion_auth_try/admin/deactivate/1
I get an 404 error.
The signature of the 'deactivate' method is
function deactivate($id = NULL)
I've been trying to solve this for a long time now. I'm stuck.
Solução
If I were you, I'd do this instead.
$route['admin/(:any)'] = "auth/$1";
It's much more simpler and it solves the problem perfectly. With the above rule, you can access both admin/some_method
and admin/some_other_method/with_a_parameter
just fine. However, you should note that if you were to access the page by entering just admin
, you'd need to add the following:
$route['admin'] = "auth";
See the doc: http://codeigniter.com/user_guide/general/routing.html