Php – Codeigniter Routing using post and get params


I have the following in my config/routes.php file:

$route['(:any)'] = "dashboard/index/$1";

This works as expected when visiting the following url:, the function in the dashboard controller is called and the 1st param is 'user' and 2nd 'id'. However when passing through post and get values such as the function appears to ignore the post params and the 1st (and only) param is '2'.

My config.php file has following values:

$config[‘uri_protocol’]  = “AUTO”;
$config[‘enable_query_strings’] = TRUE;

and .htaccess:

RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

Am I missing something in my route condition?

Best Solution

It's definitely your custom route, could you please try cause I think that the string id?page=2 gets passed as one parameter. You can also do debugging with printing out the array of passed url keys:

echo '<pre>';
echo '</pre>'

and you will get all params passed to your controller action. You can also see this answer: Handling question mark in url in codeigniter about extending the core URI class if that is needed. I hope this helps, otherwise more info will be helpful, for example what are the url keys passed in your controller when you are using normal request like and when you are doing requests.

****** EDIT ********** If you are using CI 1.7.1 or 1.7.2 then you are getting [1] => page because in the _fetch_uri_string method you have something like this:

if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
     $this->uri_string = key($_GET);

Since you have this means: $_GET['page'] = 2 and page is the name of the key in $_GET

Please use the link above about how to extend this class in your application and add your own logic here, for example something like this would work for 1.7.1 or 1.7.2

if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
     //Comment this line cause you don't need it
     //$this->uri_string = key($_GET);

     //fetch the current page number if there is one
     $page_number = (int) $_GET['page'];

     //Set the uri_string here with the proper id and pass the id (which you should get here too) and the page number as second parameter then just return; to stop execution further
     $this->uri_string = 'dashboard/index/id/'.$page_number

//In your dashboard controller:
//Set default values to 0 and process further
public function index($id=0, $page_number=0)


Hope this helps

Related Question