Welcome Guest, Not a member yet? Register   Sign In
Using query strings normally
#1

[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


Messages In This Thread
Using query strings normally - by El Forum - 09-06-2007, 06:46 AM
Using query strings normally - by El Forum - 09-06-2007, 01:23 PM
Using query strings normally - by El Forum - 09-06-2007, 01:32 PM
Using query strings normally - by El Forum - 09-06-2007, 01:54 PM
Using query strings normally - by El Forum - 09-06-2007, 04:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB