• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting CodeIgniter to work with URL parameters site,com/controller/method/?param=value

#1
[eluser]nbdr[/eluser]
I need CI to work with URL parameters as well as the regular path structures.

I read in several threads that the solution is to change:
$config['uri_protocol']="PATH_INFO"; AND $config['enable_query_strings'] = TRUE;

When I changed to $config['uri_protocol']="PATH_INFO"; only the homepage worked and all of the links mysite/page/ gives the homepage.

I then found that $_SERVER['PATH_INFO'] does not exist. in fact the query parameters only appear in REQUEST_URI and not in any other $_SERVER part.

So I checked the htaccess file and changed
RewriteRule ^(.*)$ index.php?/$1 [L]
to
RewriteRule ^(.*)$ index.php?/$1 [QSA,L] to allow url params to pass. Now url params appear also in REDIRECT_QUERY_STRING, QUERY_STRING AND REQUEST_URI.

The problem: I tried $config['uri_protocol'] with all the above options but still CI gives error 404 whenever I add URL parameters.

Note: I tried the above on 2 servers. One of them is centos5/Apache2/Plesk VPS and another one is on xampp/Vista.

#2
[eluser]CtheB[/eluser]
There is no reason you need to work with parameters.
Tell me just 1 valid reason.

You can use the "parameters" like segments as follow:

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

class MY_URI extends CI_URI {
    
    public function __construct()
    {
        parent::CI_URI();
    }
    
    public function segment($n, $no_result = FALSE)
    {
        if(is_string($n) && ($key = array_search($n,$this->segments)) !== false)
        {
            $n = $key+1;
        }
        return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
    }

    public function rsegment($n, $no_result = FALSE)
    {
        if(is_string($n) && ($key = array_search($n,$this->rsegments)) !== false)
        {
            $n = $key+1;
        }
        return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
    }

    public function slash_segment($n, $where = 'trailing')
    {
        if(is_string($n) && ($key = array_search($n,$this->segments)) !== false)
        {
            $n = $key+1;
        }
        return $this->_slash_segment($n, $where, 'segment');
    }
    
    public function slash_rsegment($n, $where = 'trailing')
    {
        if(is_string($n) && ($key = array_search($n,$this->rsegments)) !== false)
        {
            $n = $key+1;
        }
        return $this->_slash_segment($n, $where, 'rsegment');
    }
}

Now you can use $this->uri->segment("param") // This will give back "value"

#3
[eluser]nbdr[/eluser]
The reason is to track google adwords campaign, the url from google ads looks something like mysite/mypage?glcid=234524234

#4
[eluser]nbdr[/eluser]
After some trials of other suggested solutions which did not work for me I reached the simplest solution:

In htaccess file add this

RewriteCond %{QUERY_STRING} !^$
RewriteRule ^[/]?$ index.php?// [L] # if its the homepage
# for any other page remove whats beyond the ?
RewriteRule ^(.*)[?]?(.*)$ $1

Before the regular
RewriteCond %{REQUEST_FILENAME} !-f etc....

This deletes the url parameters for codeigniter. all settings in the config file remain the same ($config['uri_protocol']= "AUTO"; $config['enable_query_strings'] = FALSE; )

#5
[eluser]pbreit[/eluser]
Quote:There is no reason you need to work with parameters.

This is flat out wrong. GET is a core aspect of the web and millions of web apps use it. As a poster already pointed out, systems like Google tracking use it. Are we to tell Google to switch to parameters that we specify?

The real truth is there is no reason a framework should not provide first class support for GETs and querystrings. They are one of the most fundamental foundations of all the web.

#6
[eluser]jfedgar[/eluser]
People are just ridiculous on this board when it comes to the segmented query strings. YES THERE ARE A MILLION VALID REASONS THAT YOU NEED TO USE PARAMETERS. The primary one being that many web services (including google's subauth) return information via parameters.

The Codeigniter community needs to STOP insisting that everything is alright with the way codeigniter does query strings, and realize that there is a large portion of the community spending hours of time trying to implement workarounds to something that should be a NON-ISSUE and would take 15 seconds to implement with Raw PHP.

#7
[eluser]InsiteFX[/eluser]
Code:
$str_get = parse_str($_SERVER['QUERY_STRING'], $_GET);

//Remove everything before (and including)
//the question mark and then parse into $_GET array.
parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
echo ' Using rebuilt $_GET array with xss protection: ' .
       $this->input->xss_clean($_GET'foo']);

InsiteFX

#8
[eluser]jfedgar[/eluser]
the problem becomes much more complicated than that when you are working on a server that has path_info disabled (which many of the big hosts seem to) and they will not let you enable it.

Several solutions to the problem are here: http://ellislab.com/forums/viewthread/159382/

I have mine working now, but it should not be so involved for something so fundamental.

#9
[eluser]pbreit[/eluser]
As Insite points out there may be workarounds. But it's really unforgivable that querystings are not supported out-of-the-box. This should not require a work-around. It's one of the main reasons I have left CodeIgniter.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.