CodeIgniter Forums
Using query strings normally - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Using query strings normally (/showthread.php?tid=3039)



Using query strings normally - El Forum - 09-06-2007

[eluser]Paul Scott[/eluser]
I found myself wanting to be able to specify a variable through GET as-well as through CI's ability to parse the / into parameters of the called function. I was wanting to do this because I was passing a site-wide parameter (it was checked for no matter what page you were on) and I didn't want to have to add the parameter to every function - especially not when some functions had optional parameters.

I found, to my dismay, that trying to go to a url like /admin/main/index?msg=Something returned a 404. A little inspection found that CI was trying to find the controller and function specified by 'msg'.

To fix this, I opened ./system/libraries/Router.php and in the function `_get_uri_string` there is a section that checks if there is only 1 GET variable and uses the name of it to find the controller.
Code:
// If the URL has a question mark then it's simplest to just
// build the URI string from the zero index of the $_GET array.
// This avoids having to deal with $_SERVER variables, which
// can be unreliable in some environments
if (is_array($_GET) AND count($_GET) == 1)
{
    // Note: Due to a bug in current() that affects some versions
    // of PHP we can not pass function call directly into it
    $keys = array_keys($_GET);
    return current($keys);
}
This is the code block that did it within the `_get_uri_string` function. To counter-act this, I simply changed the if-statement to read
Code:
if (FALSE AND is_array($_GET) AND count($_GET) == 1)
Then I needed to stop the GET variable from being destroyed by CI and stop CI trying to take the controller and function from the `c` and `m` $_GET variables by default:
Code:
$config['enable_query_strings'] = TRUE;
$config['controller_trigger'] = '';
$config['function_trigger'] = '';

Now I can use URIs like /admin/clients/view/613?msg=updated with my header-template checking for $_GET['msg'] and out-puting it on any page that it's found on.

I thought I would post this because somebody else might have this problem and was wondering why this is the default behaviour? Also, is there a reason why this might have been a bad idea?

Thanks


Using query strings normally - El Forum - 09-06-2007

[eluser]Developer13[/eluser]
If you want to pass that variable through the uri, why don't you do:

/admin/clients/view/613/msg/updated

?


Using query strings normally - El Forum - 09-06-2007

[eluser]Paul Scott[/eluser]
I was thinking of that, but that wouldn't allow for me to not specify a message because doing so would make me send 'updated' into the 'msg' parameter. I also didn't want to put it on the end because there are optional parameters in some of the functions that I didn't want to specify all of the time, either.

This seemed like the best method other than parsing the segments before the controller is called and removing and using segments that match a form something like:
Code:
/msg:Updated/
And then storing them in a GET-equivelant array. This method would be the nicer of the two, but would require more fiddling with the main CI files.


Using query strings normally - El Forum - 09-06-2007

[eluser]Developer13[/eluser]
I'm really sorry if I'm being thick-headed here or not understanding what you're really trying to accomplish... but if you don't have a msg variable to pass then you just shouldn't include it.

I use uri_to_assoc
frequently. This would allow your controller to check for optional parameters that might be passed as a variable through the URI.

If your controller expects the possibility of var1, var2, var3, var4 and / or var5 and your URI contained: /admin/clients/view/123/var1/var1_value/var3/var_3value and you used $this->uri->uri_to_assoc(3), you would have an array as follows:

array (
'view'=>'123',
'var1'=>'var1_value',
'var3'=>'var3_value')

Your controller can still check for var2 and var5 and act accordingly. Since it has var1 and var3, it can then do what it needs to with those.

I think GET variables were purposely disabled in CI for a purpose, and I think you'll just have a harder time with your app(s) when you have to spend time modifying some of the core files. That's just me. There are many ways to get around using GET with CI.


Using query strings normally - El Forum - 09-06-2007

[eluser]Paul Scott[/eluser]
Yes, $this->uri->uri_to_assoc would have also been useful for me there, but I'd have still had to adapt every function in all of my controllers to work with it.

Thanks for showing me that, all the same. I'm still not too familiar with all of the CI libraries and helpers.