Welcome Guest, Not a member yet? Register   Sign In
Query String URL Enhancement
#5

[eluser]domonline[/eluser]
Thought I share this as I found it incredibly frustrating that I couldn't mix using paths and query strings and a situation arose that demanded this with very short time frame to sort it out.

If you want urls like:

Code:
http://www.yoursite.com/segment/and_so_on/?query=1&query1=2

This'll work like you'd expect it.

e.g. you can still use :

Code:
$this->uri->segment(2)

// but you can also use

$_GET['query']

and

Code:
http://www.yoursite.com/segment/and_so_on // Works
http://www.yoursite.com/index.php?c=food&m=menu // Works and pulls up crontroller/method
http://www.yoursite.com/segment/and_so_on/?query=working // Also works

You need to create a file called MY_URI.php in your system/application/libraries/ folder

Put the following in it, it's really simple just overrides one method.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_URI extends CI_URI {

    function MY_URI() {
        parent::CI_URI();
    }

    // --------------------------------------------------------------------
    
    /**
     * Override the _parse_request_uri method so it allows query strings through
     *
     * @access    private
     * @return    string
     */    
    function _parse_request_uri()
    {
        if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
        {
            return '';
        }
        
        $uri = explode("?",$_SERVER['REQUEST_URI']); // This line is added to the original
        $request_uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $uri[0])); // This line changed
        // Everything else is just the same

        if ($request_uri == '' OR $request_uri == SELF)
        {
            return '';
        }
        
        $fc_path = FCPATH;        
        if (strpos($request_uri, '?') !== FALSE)
        {
            $fc_path .= '?';
        }
        
        $parsed_uri = explode("/", $request_uri);
                
        $i = 0;
        foreach(explode("/", $fc_path) as $segment)
        {
            if (isset($parsed_uri[$i]) AND $segment == $parsed_uri[$i])
            {
                $i++;
            }
        }
        
        $parsed_uri = implode("/", array_slice($parsed_uri, $i));
        
        if ($parsed_uri != '')
        {
            $parsed_uri = '/'.$parsed_uri;
        }

        return $parsed_uri;
    }

}

?>

In your system/application/config.php file you need to enable query strings so $_GET isn't unset and set the uri protocol to REQUEST_URI. With some digging around I'm sure you could get AUTO and the other methods to work as well but just didn't have the time or the need.

Code:
$config['uri_protocol']    = "REQUEST_URI";
$config['enable_query_strings'] = TRUE;

I hope this is of help to someone


Messages In This Thread
Query String URL Enhancement - by El Forum - 01-11-2008, 02:17 PM
Query String URL Enhancement - by El Forum - 02-16-2008, 02:10 PM
Query String URL Enhancement - by El Forum - 02-19-2008, 04:09 PM
Query String URL Enhancement - by El Forum - 03-13-2008, 01:13 PM
Query String URL Enhancement - by El Forum - 04-23-2008, 05:31 PM
Query String URL Enhancement - by El Forum - 04-23-2008, 07:41 PM
Query String URL Enhancement - by El Forum - 04-24-2008, 12:07 AM
Query String URL Enhancement - by El Forum - 09-01-2010, 02:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB