• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
URI - getting segments into associative array with user-defined keys

OK, $this->uri->uri_to_assoc(n) - is great: from a URI like:


we end up with an array like:

    'name' => 'joe'
    'location'    => 'UK'
    'gender'    => 'male'

(taken from the docs)

...BUT I don't really want that great big URI, with the parameter NAMES in - I mean I know which parameter is which. It would make for better code if there was a class that would let me specify parameter names:

$paramNames = array ('name', 'location', 'gender');

and then match the URI segments in corresponding order, using these as key names. That way I would still end up with

'name' => 'joe'
'location' => 'UK'
'gender' => 'male'

...but from a URI in the form:


i.e. without the parameter names appearing in the URI. Then I could use e.g.

$name = $uriArray ['name'];

...which is obviously nicer than referring to the parameters by segment number.

I am pretty sure there isn't a helper available to achieve this, am I right? Is there any reason for that, if not? I know it would be easy to write, just wondering... Suggestion for a future addition?

[eluser]Jamie Rumbelow[/eluser]
There isn't anything like that currently, but it could be easily implemented. The only issue here is where would you put something like this? CodeIgniter's routing system is very simple, so it wouldn't make sense to put it in the routes.php config file. It would be messy if it was inline in the function code. It would be too cumbersome to put in the doccomment or somewhere else in the controller.

Where could it go?

What about just expanding uri_to_assoc to take an array as an extra optional parameter, like

uri_to_assoc (3, array('name', 'location', 'gender'))

so that if this optional array is specified then these keys get assigned to the URI values, if not then the default behaviour is applied. Though that's a bit messy because of the different size URIs depending on the case. Perhaps just another function in the URI class called uri_to_userassoc, or something, which would work as above?

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.