Hi everybody,
I had a problem described in my last post:
http://ellislab.com/forums/viewthread/68197/
This problem forced me to use query strings and the url I had to parse was as follows:
http://www.mysite.com/index.php?c=mycontroller&m=processAuth&auth_token=3c562b119bd387ec194ade6bab9832c4
I enabled query strings in my config file:
$config[‘enable_query_strings’] = TRUE;
$config[‘controller_trigger’] = ‘c’;
$config[‘function_trigger’] = ‘m’;
I still had issues getting to the auth_token value.
In the end I changed some code in the Router.php file and wanted to submit the code here in case anyone else finds it useful. It makes query strings act pretty much the same as the regular CodeIgniter URLs. For example, if you had a controller like:
class myClass extends Controller {
function myFunction ($myValue) {
}
}
Normally, you could call myFunction with a value using a CodeIgniter URL:
<a href="http://www.mysite.com/index.php/myClass/myFunction/123456">http://www.mysite.com/index.php/myClass/myFunction/123456</a>
With the changes I made to router.php, you can now call it with:
<a href="http://www.mysite.com/index.php?c=myClass&m=myFunction&value=123456">http://www.mysite.com/index.php?c=myClass&m=myFunction&value=123456</a>
The way the changed code is written now, you only need the “controller_trigger” key in the query string. The other keys do not matter. The values will go into the segment array in the order they show up in the query string.
There are a few improvements that could be made but what’s there now is a good start.
I can’t seem to attach the changed Router.php file (renaming or zipping didn’t seem to matter). But I only changed the _set_route_mapping() function commenting out the following code at the top:
/*
// Are query strings enabled in the config file?
// If so, we're done since segment based URIs are not used with query strings.
if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
{
$this->set_class(trim($this->_filter_uri($_GET[$this->config->item('controller_trigger')])));
if (isset($_GET[$this->config->item('function_trigger')]))
{
$this->set_method(trim($this->_filter_uri($_GET[$this->config->item('function_trigger')])));
}
return;
}
*/
and adding the following code near the end of the function:
// check if query strings enabled
if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
{
// at this point the uri_string is the query string
$keyValues = split('&', $this->uri_string);
foreach($keyValues as $keyValue) {
list($key, $value) = split('=', $keyValue, 2);
// just use the value and add in the order they come in
// at some point may want to utilize keys and ignore order
$value = trim($this->_filter_uri($value));
if ($value != '')
$this->segments[] = $value;
}
} else {
the code above goes just above:
// Explode the URI Segments. The individual segments will
// be stored in the $this->segments array.
foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
{
// Filter segments for security
$val = trim($this->_filter_uri($val));
if ($val != '')
$this->segments[] = $val;
}
} // do not forget this bracket
make sure to add the closing bracket
Hope someone finds it useful. 😊