views:

134

answers:

3

I understand that CI is mostly URL segment based, but I want to have a query string: blahblah.com/search.html?q=keyword

When I try $this->input->get( "q" ), it returns empty. Is there a route or something I need to configure?

+1  A: 

Why not make it http://mysite.com/search/keyword/

Tom Schlick
There are multiple inputs in a form.. so how would that be handled?
Shamoon
He could do it as a submit action="post" then redirect to /search/keyword but whats the point in that?
Phil Sturgeon
+1  A: 

You have to enable query strings

CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you'll see these items:

$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';

If you change "enable_query_strings" to TRUE this feature will become active. Your controllers and functions will then be accessible using the "trigger" words you've set to invoke your controllers and methods:

index.php?c=controller&m=method

Example: index.php?c=products&m=view&id=345

http://codeigniter.com/user_guide/general/urls.html

Sylvio
That makes for some disgusting URL's and would screw with your entire app just to get one search form working.
Phil Sturgeon
Very true Phil, I just answered his question:-) next time I'll advice also
Sylvio
A: 

The best way to get query strings working in CodeIgniter is to use Google. This question is asked (and answered) on the forums, here and on twitter at least 10 times a day.

There are a few methods, but recently I am a fan of the following method:

http://www.dijexi.com/2009/08/how-to-mix-segment-and-query-string-in-codeigniter/

I prefer this over others as it will have no application-wide effects like some other approaches and it won't require any crazy hacking to get it working.

If you want this $_GET support throughout the entire app, just put the parse_str into MY_Controller or a pre_controller hook.

Phil Sturgeon
Thanks Phil. The problem here is that when I set to "PATH_INFO", none of my routing seems to work.
Shamoon
Ahh great you are using a CGI based PHP installation? Try adding ini_set('cgi.fix_pathinfo', 1); to your index.php. Failing that, modify your .htaccess to use RewriteRule ^(.*)$ index.php?/$1 [L] .otice the ? before /$1, this is needed for CGI based installs.
Phil Sturgeon