How can I use GET forms with CodeIgniter?

2019-01-27 07:17发布

问题:

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:

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.



回答2:

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



回答3:

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