![]() |
How to Process Params of a Route Callback Function? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6) +--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17) +--- Thread: How to Process Params of a Route Callback Function? (/showthread.php?tid=65653) |
How to Process Params of a Route Callback Function? - grins - 07-06-2016 Id like to have the following routes: mydomain.com/controller/specific_method/any_param => remains as is mydomain.com/controller/specific_param => mydomain.com/controller/index/specific_param Ive set up the following route: Code: $route['controller/(:any)/(:any)'] = function ($a, $b) { The results of navigating to the various links are: mydomain.com/controller/specific_method/param => the method is loaded but the param is ignored. mydomain.com/controller/specific_param_for_index => 404 error, file not found. It appears that the 'index/' is not inserted into the url. Assuming the controller is properly setup with index and specific_method ready to process their params, what might I be doing wrong here? RE: How to Process Params of a Route Callback Function? - Muzikant - 07-07-2016 Hi. If you did not, read the URI Routing section in the manual: http://www.codeigniter.com/user_guide/general/routing.html There should be everything you need to know about routes in CodeIgniter. RE: How to Process Params of a Route Callback Function? - InsiteFX - 07-07-2016 See also the uri->segment() method in the uri class in core RE: How to Process Params of a Route Callback Function? - grins - 07-07-2016 (07-07-2016, 01:09 AM)Muzikant Wrote: Hi. If you did not, read the URI Routing section in the manual: Thank you. Ive been referencing the manual constantly. The section of that page that refers to callbacks provides a very simple example and no other details. Based on it, I assume that I should be able to process the parameters and return a string that represents my rerouted path. I believe I do just that in my example, but my results are not as expected. Either my PHP has an error or Im not quite understanding how to use callbacks in this case. Whichever it is, Im not seeing the issue. RE: How to Process Params of a Route Callback Function? - grins - 07-07-2016 (07-07-2016, 03:30 AM)InsiteFX Wrote: See also the uri_segment() method in the url_helper Thank you, I will review that subject today and see if I can the issue. RE: How to Process Params of a Route Callback Function? - mwhitney - 07-07-2016 For the second problem, try setting a default value for $b PHP Code: $route['controller/(:any)/(:any)'] = function ($a, $b = '') { The code which adds 'index' to the URI segments if it isn't found appears to be executed after the callback. Once the callback returns a string, the Router attempts to resolve the string to a controller/method/parameters. Another option might be simpler, though. You should be able to do something like this: PHP Code: $route['controller/specific_method/(:any)'] = 'controller/specific_method/$1'; As long as the 'controller/specific_method' route is defined first, it should be resolved before the 'controller/(:any)' routes. RE: How to Process Params of a Route Callback Function? - grins - 07-07-2016 (07-07-2016, 08:02 AM)mwhitney Wrote: Another option might be simpler, though. You should be able to do something like this: Thank you for the answer! I had to go with the simpler option as the callback function just refused to work as expected even if the 2nd param had a default empty value. |