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

[eluser]Pygon[/eluser]
[quote author="badgeek" date="1204358813"]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]
[/quote]

This will work great if you only want query strings to go to a specific controller. You can actually do this quite a bit easier by simply using:

index.php?main&search&myquerystr;=what+ever

Lovecannon is correct that there are other ways to use GET strings, however my main attempt was to have a drop-in solution that doesn't require configuration or base file changes to work on any implementation of CI.
#12

[eluser]Lovecannon[/eluser]
Yeah, a drop-in replacement also works, but personally, I think its a better practice to set the URI protocol to PATH_INFO just be sure things dont break when a query string is used.
#13

[eluser]Pygon[/eluser]
I would disagree on a personal level, simply that I don't find it better to force PATH_INFO usage. This won't work correctly if "$config['enable_query_strings'] = TRUE;" is set, without making a change to the uri_protocol.

Regardless, same results with a different implementation. To each his/her own.
#14

[eluser]Avatar[/eluser]
I get an error when running your code:
Code:
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: libraries/MY_Input.php

Line Number: 35
#15

[eluser]xwero[/eluser]
I wonder why the URI library is hacked the $_GET global is set to an empty array in the Input class (from line 110)
Code:
if ($this->allow_get_array == FALSE)
        {
            $_GET = array();
        }
        else
        {
            if (is_array($_GET) AND count($_GET) > 0)
            {
                foreach($_GET as $key => $val)
                {
                    $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
                }
            }
        }
So all you need to to do is set $_GET = array(); in comment and of you go Smile
#16

[eluser]Pygon[/eluser]
unfortunately no xwero

CI_URI/CI_Router keys off $_GET[0] and $_GET[1] prior to CI_Input class ever loading, so /index.php/1/2?3&4;will actually take you to controller 3, function 4.
#17

[eluser]Pygon[/eluser]
[quote author="yingyes" date="1204724364"]I get an error when running your code:
Code:
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: libraries/MY_Input.php

Line Number: 35
[/quote]

Hi yingyes --

This isn't quite enough info, although you can test the following.

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
            if(is_array($_GET) && count($_GET)>0)
            {
                foreach($_GET as $key => $val)
                {
                    $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
                }
            }
            else
            {
                $_GET = array();
            }
        }
    }
}
?>

Let me know if this fixes your issue.
#18

[eluser]xwero[/eluser]
[quote author="Pygon" date="1204743806"]unfortunately no xwero

CI_URI/CI_Router keys off $_GET[0] and $_GET[1] prior to CI_Input class ever loading, so /index.php/1/2?3&4;will actually take you to controller 3, function 4.[/quote]
I just tested it with the urls like /index.php/some/segment?one=1&two=2&three=3 /index.php/some/segment/?one=1&two=2&three=3 and to check i added to the method following
Code:
var_dump($this->input->get('one'));
var_dump($this->input->get('two'));
var_dump($this->input->get('three'));
and i got this result
string(1) "1"
string(1) "2"
string(1) "3"

The only reason i could think about why CI cuts off the fist two segments is because the enable_query_strings config variable is set to true.
#19

[eluser]Avatar[/eluser]
still not working for m, it looks as though it's not even setting the $_GET variable because of enable_query)strings being set to true and in the CI Base Input class it does this:

Code:
if ($this->allow_get_array == FALSE)
        {
            $_GET = array();
        }
#20

[eluser]Pygon[/eluser]
yingyes:

I'm a bit confused as to how you are attempting to make this work because your description is vague. In fact, I'm not sure why you would even be posting anything here if you are using enable_query_strings as true, since that would completely bypass any of my code.

xwero:

My appologies -- try only: index.php/some/segment/?one=1 and you should recieve a 404 since CI_URI assumes that if count($_GET) == 1, the first key is the name of the base controller you would like to access:

CI_URI.php
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)
{
    $this->uri_string = key($_GET);            
    return;
}

However, from what you've said, and reviewing the code, I've realized that CI_URI could simply be modified.

Using enable_query_strings = true and uri_protocol = auto, GET requests should work perfectly fine as long as there is more than one GET variable.

I suspect if CI_URI was extended and that section of code commented out, everything should work the same, although I haven't exactly determined WHY this zero index thing would be done anyway.

Could be accomplished with:
$config['enable_query_string'] = true;
$config['uri_protocol'] = 'AUTO';

MY_URI.php
Code:
<?php
class MY_URI extends CI_URI {
    var $_backup_get;
    
    function MY_URI()
    {
        parent::CI_URI();
    }
    
    function _fetch_uri_string()
    {
        if (is_array($_GET) AND count($_GET) == 1)
        {
            $this->_backup_get = $_GET;
            $_GET = array();
            parent::_fetch_uri_string();
            $_GET = $this->_backup_get;
            return;
        }
        parent::_fech_uri_string();        
        return;
    }
}
?>

course you lose the "single _GET not using _SERVER variable performance benefit" thing.




Theme © iAndrew 2016 - Forum software by © MyBB