Welcome Guest, Not a member yet? Register   Sign In
quick question about URI parameters
#1

[eluser]KeyStroke[/eluser]
Hi,

When using URI parameters instead of GET variables, do all the segments following the controller's method name in the URI need to be key/value pairs? because I don't see how URI parameters would work with a method that has an optional parameter (or even a required one for that matter, since it's only a value, without a key).

Thanks in advance Smile
#2

[eluser]pistolPete[/eluser]
Have a look at the user guide: Passing URI Segments to your Functions
#3

[eluser]KeyStroke[/eluser]
I'm afraid that's not what I meant.

I meant URIs where parameters are used like this:
Quote:http://example.com/index.php/controller/...rt_by/date
and then using '$this->uri->uri_to_assoc()' to extract the key/value pairs off the URI.
#4

[eluser]TheFuzzy0ne[/eluser]
How frustrating! I know the answer but I can't word it in a way that would be understood by anyone other than myself...

Basically, URLs generally follow a hierarchical structure, and the arguments are always passed into the methods in the same, static manner. Sometimes you will need to be able to have key/value pairs in order to pass information that does not adhere to the same structure. For times like this, you can use $this->uri->uri_to_assoc(), which is available in the uri library, which is conveniently loaded for you automatically. More information on the library and function can be found [url="http://ellislab.com/codeigniter/user-guide/libraries/uri.html"]here[/url]
#5

[eluser]TheFuzzy0ne[/eluser]
OK, I'm lost. I'd could have sworn you'd just answered your own question.
#6

[eluser]KeyStroke[/eluser]
I'm just asking how to deal with situations where I'm passing key/value pairs in the URI to a function that has optional parameters (i.e: variables defined in its declaration).

I figured it out though. I think i'll just do something like this:
Code:
function foo($bar = NULL)
{
   if($bar == NULL)
   {
      $vars = $this->uri->uri_to_assoc(4);
   }
   else
   {
      $vars = $this->uri->uri_to_assoc(); // since default is 3
   }

This way I could hopefully avoid such conflicts.
#7

[eluser]TheFuzzy0ne[/eluser]
Quite, but generally that can be avoided by providing that first parameter as part of a key/value pair.
#8

[eluser]KeyStroke[/eluser]
You just answered my initial question by the way. Tongue

So you advise that any functions that use URI key/value pairs should not have parameters?
#9

[eluser]TheFuzzy0ne[/eluser]
Yes, wherever it can be avoided, anyway. If you're using that method, why mix it with a method that's not compatible? If there's no good reason to do it, why do it? There may be a time when you can't avoid using it, but I can't think of any scenario where you'd have to use it.

Hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB