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

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) {
 $r = 'controller/';
 switch($a) {
   case'specific_method':
     $r .= $a.'/'.$b;
   break;
   case'specific_param_for_index':
     $r .= 'index/'.$a.'/'.$b;
   break;
 }
 return $r;
};

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?
Reply
#2

(This post was last modified: 07-07-2016, 01:10 AM by Muzikant.)

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.
Reply
#3

(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 )
Reply
#4

(07-07-2016, 01:09 AM)Muzikant Wrote: 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.

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.
Reply
#5

(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.
Reply
#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
#7

(07-07-2016, 08:02 AM)mwhitney Wrote: 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.

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB