How to Process Params of a Route Callback Function? |
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?
Hi. If you did not, read the URI Routing section in the manual:
http://www.codeigniter.com/user_guide/ge...uting.html There should be everything you need to know about routes in CodeIgniter.
07-07-2016, 03:30 AM
(This post was last modified: 07-07-2016, 10:27 AM by InsiteFX. Edit Reason: was a class not the helper )
See also the uri->segment() method in the uri class in core
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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.
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. (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. |
Welcome Guest, Not a member yet? Register Sign In |