Welcome Guest, Not a member yet? Register   Sign In
SEO URIs *AND* $_GET requests - /controller/function/?myvar=1
#1

[eluser]Pygon[/eluser]
After being annoyed for the longest time with CI's handling of _GET requests while using SEO friendly URIs, I decided to come up with a solution.

So far, I've not run into any problems, however this is fairly fresh and lightly tested.



app/lib/MY_URI.php
Code:
<?php
class MY_URI extends CI_URI {
    
    function MY_URI()
    {
        $this->config =& load_class('Config');

        //Config using query strings?
        if( $this->config->item('enable_query_strings') !== TRUE )
        {
            //Save a copy of $_GET for later.
            global $_backup_get;
            $_backup_get = $_GET;

            //Clean any references URI might use
            $_GET = array();
            $_SERVER['QUERY_STRING'] = "";
        }

        parent::CI_URI();
    
    }
}
?>

app/lib/MY_Input.php
Code:
<?php
class MY_Input extends CI_Input {
    
    function MY_Input()
    {
        parent::CI_Input();

        $this->config =& load_class('Config');
        
        //Using query strings?
        if($this->config->item('enable_query_strings') !== TRUE)
        {
            //Process as normal
            parent::CI_Input();

            global $_backup_get;
            
            //Repopulate $_GET from URI
            $_GET = $_backup_get;

            unset($_backup_get);
            
            //Clean  $_GET Data
            foreach($_GET as $key => $val)
            {
                $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
            }
        }
    }
}
?>

Access as normal using $this->input->get()
#2

[eluser]frenzal[/eluser]
I'll give it a go a later, it's allways handy to have access to GET data especially when dealing with 3rd parties, thanks!
#3

[eluser]jmun[/eluser]
I am trying:

http://mydomain/test/form/?myvar=1

Code:
function form()
{
  $test = $this->input->get();
  var_dump($test);
}

But it returns false. Any ideas?
#4

[eluser]xwero[/eluser]
[quote author="jmun" date="1204159789"]I am trying:

http://mydomain/test/form/?myvar=1

Code:
function form()
{
  $test = $this->input->get();
  var_dump($test);
}

But it returns false. Any ideas?[/quote]
The get method from the input class doesn't mimic the get global. The method retrieves on value based on a key you add as argument. So
Code:
function form()
{
  $test = $this->input->get('myvar');
  var_dump($test);
}
should give you the expected result
#5

[eluser]Lovecannon[/eluser]
It takes one line to enable get...without having to override anything..just add this to anywhere you want $_GET...
Code:
parse_str($_SERVER['QUERY_STRING'],$_GET);
#6

[eluser]Neophyte[/eluser]
[quote author="Lovecannon" date="1204187790"]It takes one line to enable get...without having to override anything..just add this to anywhere you want $_GET...
Code:
parse_str($_SERVER['QUERY_STRING'],$_GET);
[/quote]

With that approach any URI in the form of /controller/method/?q=Search+Terms+Here will not work as CI will no longer grab the controller and method segments. So you'll probably end up with a 404 error.

Pygons approach initially clears any GET data so CI will parse out the segments allowing CIs routing to work as expected then a little further on in execution once CI has decided on all the segments and knows the controller and method to be called the GET data is re-initialised ready for use with the input library.
#7

[eluser]Lovecannon[/eluser]
I use the above parse_str method all the time, and it never causes an error in Codeigniter. It works just fine.
#8

[eluser]jmun[/eluser]
I tried parse_str($_SERVER['QUERY_STRING'],$_GET); in my controller/function and I get a 404 error.

I used Pygon's code and it seems to work pretty well.
#9

[eluser]Lovecannon[/eluser]
Well, I bet you have your URI protocol set to AUTO, open your application/config/config.php and change the $config['uri_protocol'] to PATH_INFO, but PATH_INFO is not correctly set if your PHP installation runs on FastCGI, so you could either change PATH_INFO to ORIG_PATH_INFO or add this line to your CI index.php

Code:
$_SERVER['PATH_INFO'] = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : '';
#10

[eluser]badgeek[/eluser]
i use this on my site, just enable query string on your config

Code:
RewriteEngine On
    RewriteBase /leakrv2/
    RewriteCond %{query_string} ^q=(.*)$
    RewriteRule ^search/$ /leakrv2/index.php/?c=main&m=search&q;=%1 [L]




Theme © iAndrew 2016 - Forum software by © MyBB