Work To GET method on CodeIgniter

Enable GET method on CodeIgniter Frame Work with SEF urls

CodeIgniter is a powerful PHP framework with a very small footprint,built for specially PHP coders.It has many advantages like SEF URL ,light weight, high level Security,MVC model ,more libraries .
But it has strictly followed SEF segments ,it unsets or delets the global $_GET array to prevent Security Hacks when it initializes .To Enable or Create Custom GET method
open config.php file in aplications/config/ folder

set uri protocal

 $config['uri_protocol']	= "PATH_INFO";

code sample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string.  The default setting of "AUTO" works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO'			Default - auto detects
| 'PATH_INFO'		Uses the PATH_INFO
| 'QUERY_STRING'	Uses the QUERY_STRING
| 'REQUEST_URI'		Uses the REQUEST_URI
| 'ORIG_PATH_INFO'	Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol']	= "PATH_INFO";

add a line of code in your control method to recreate the GET array
for example codeigniter default welcome.php controller in Codeigniter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Welcome extends Controller {
 
	function Welcome()
	{
		parent::Controller();	
	}
 
	function index()
	{	
		parse_str($_SERVER['QUERY_STRING'],$_GET); //converts query string into global GET array variable
		print_r($_GET); //test the $_GET variables
		$this->load->view('welcome_message');
	}
}

if you run your code with following URL


http://localhost/index.php/welcome/?var1=1&var2=2&var3=3

The result will be

Array
(
    [var1] => 1
    [var2] => 2
    [var3] => 3
)

Here parse_str function converts string to array with given deliminator.To know more about parse_str go to php.net
Even though we can achieve this result, i suggest you to stick to segments because it more user friendly.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">