1

Say user is going to search on 4 or 5 fields, ok? eg. he might want to search by firstname, email, mobile or even page number

I want that codeigniter's url be like this:

site.com/Controller/Method/variable1/value1/variable2/value2/variable3/value3

or

site.com/Controller/Method/variable2/value2/variable3/value3/variable1/value1

(that they should have the same result)

or in this format

site.com/Controller/Method/variable2/value2/variable4/value4

some examples to clarify my question:

site.com/user/search/firstname/John/mobile/123/page/2

or:

site.com/user/search/lastname/Smith/email/gmail.com

In one sentence: I don't want to force the user to use a specific order when setting value to parameters.

Mohammad Naji
  • 5,372
  • 10
  • 54
  • 79

2 Answers2

4

You can use the _remap function on the controller.

function _remap( $method, $params )
{
    $map = array();
    for( $i = 1; $i < count( $params ); $i = $i + 2 )
    {
        $map[$params[$i-1]] = $params[$i];
    }

    if( $method[0] != '_' && method_exists( $this, $method ))
        return $this->$method( $map );
}

If you want to use it across all your controllers, you would be better to write a custom controller to extend CI_controller with this function, and have all your controllers extend that.

Alex M
  • 3,506
  • 2
  • 20
  • 23
  • But there's still a question: why did you code the last if? It should work without that. We are giving other parameter/value pairs as an array to $method, but why should we need the final if? – Mohammad Naji Jan 15 '12 at 15:58
  • 1
    In code igniter, methods that a prefixed with an underscore are private in controllers. You're not supposed to be able to run them from the URL. Like the _remap function. Without it you can call _remap and others from the URL (http://yoursite.com/controller/_remap/), which would lead to an infinite loop. – Alex M Jan 15 '12 at 16:00
0

I'd pass all the parameters as 1 string with separators eg. /username|alunr#category|99 etc. and find the values from there using php's explode() function.

Mohammad Naji
  • 5,372
  • 10
  • 54
  • 79
AlunR
  • 455
  • 3
  • 10