Problem passing parameter via GET MVC

2

I can not get the values by GET within a controller or action , example:

Does not work:

example.com/controller/?q=nome
example.com/controller/action/?q=nome

It works:

example.com/?q=nome

OBS: POST type parameters work normally

.htaccess

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
    
asked by anonymous 21.05.2014 / 19:36

1 answer

1

The following is happening:

index.php?url=/controller/action/

then your $_GET['url'] is filled with this address url=/controller/action/ !!!

To recover effectively use $_SERVER['REQUEST_URI'] that it will bring: /controller/action/?q=nome .

Ready now, just work with this value:

1) Simple example:

$url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI']: '';
if ($url != ''){
    $urls  = explode('/', $url);
    $param = str_replace('?','', end($urls));
    $param = explode('&', $param);
    $params = array();
    foreach($param as $pa){
        $vp = explode('=', $pa);            
        $params[$vp[0]] = sizeof($vp)==2?$vp[1]:NULL;
    }
    //só para imprimir valor na tela!!! var_dump
    var_dump($params);
}

Result:

2)Examplewith < strong> parse_url

var_dump(parse_url($_SERVER['REQUEST_URI']));

Result:

array(2) { ["path"]=> string(19) "/controller/action/" ["query"]=> string(6) "q=nome" }

Reference

22.05.2014 / 05:22