Welcome Guest, Not a member yet? Register   Sign In
How to Process Params of a Route Callback Function?
#6

(This post was last modified: 07-07-2016, 08:03 AM by mwhitney.)

For the second problem, try setting a default value for $b

PHP Code:
$route['controller/(:any)/(:any)'] = function ($a$b '') {
    
$r 'controller/';
    switch (
$a) {
        case 
'specific_method':
             
$r .= $a.'/'.$b;
            break;
        case 
'specific_param_for_index':
             
$r .= 'index/'.$a.'/'.$b;
            break;
    }
    return 
$r;
}; 

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';
$route['controller/(:any)/(:any)'] = 'controller/index/$1/$2';
$route['controller/(:any)'] = 'controller/index/$1'

As long as the 'controller/specific_method' route is defined first, it should be resolved before the 'controller/(:any)' routes.
Reply


Messages In This Thread
RE: How to Process Params of a Route Callback Function? - by mwhitney - 07-07-2016, 08:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB