2

In my codeigniter controller I have only index function with an optional argument. If argument exists I load a view, otherwise I load another one. I use this _remap function:

function _remap($method){
    $param_offset = 2;

    // Default to index
    if ( ! method_exists($this, $method))
    {
        // We need one more param
        $param_offset = 1;
        $method = 'index';
    }

    // Since all we get is $method, load up everything else in the URI
    $params = array_slice($this->uri->rsegment_array(), $param_offset);

    // Call the determined method with all params
    call_user_func_array(array($this, $method), $params);
} 

The problem is that when I check the argument in the index function it has always value equals to 0 that is the value I set as default.

pindol
  • 2,110
  • 6
  • 35
  • 52

1 Answers1

3

Here's the original signature of the _remap() function:

function _remap($method, $params = array());

Look in $params array instead of using URI library and you're good to go.

sepehr
  • 17,110
  • 7
  • 81
  • 119
  • No it doesn't work because method contain the name of optional argument and $params in empty. Also if I set $params[] = $method it doesn't work. – pindol Dec 24 '12 at 15:23
  • Can you please provide a few examples of the routing behavior you want to achieve? e.g. `controller/test_method/123 ==> controller->index('test_method', 123)` – sepehr Dec 24 '12 at 15:29
  • Yes. I can do 2 example: One without the argument: domain.com/ci_folder/projects ==> projects->index(no args) and the other with one arg is domain.com/ci_folder/projects/project-name ==> projects->index(project-name) – pindol Dec 24 '12 at 18:31
  • Solved. I didn't use the arg variable as array, but as a simple string var. – pindol Dec 25 '12 at 10:28
  • yeah it work on my problem too, [doc remap](https://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping). I think it's good to mark the answer as the correct one. It's remain open this time :) – Adi Prasetyo Apr 30 '16 at 02:15