How to Make Codeigniter Accept "Query String" Urls

How to make CodeIgniter accept query string URLs?

For reliable use of query strings I've found you need to do 3 things

  1. In application/config/config.php set $config['enable_query_strings'] = true;
  2. Again in application/config/config.php set $config['uri_protocol'] = "PATH_INFO";
  3. Change your .htaccess to remove the ? (if present) in the rewrite rule

I use the following

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Codeigniter Query Strings URL Issue

You can prepare your config like this:

$config['base_url'] = 'your_base_url';
$config['index_page'] = ''; // empty string
$config['enable_query_strings'] = FALSE; // keep it to default FALSE, you can use it even without enabling it here

and your .htaccess like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

I think that is it, now you can use both segments and query_strings like this:

http://localhost/codeigniter/?c=welcome&m=index
http://localhost/codeigniter/welcome/index
http://localhost/codeigniter/index.php/welcome/index
http://localhost/codeigniter/index.php?c=welcome&m=index
http://localhost/codeigniter/index.php/?c=welcome&m=index

I've already tested both in my environment and it works just fine.


Edit: in my codeigniter template i've extended my core router and slightly changed the _set_routing() method like this:

Of course it is a bad practice to change the core system, so you need to extend the core router file and create MY_Router.php under application/core and make it extend CI_Router like this:

class MY_Router extends CI_Router
{
protected function _set_routing()
{
if (file_exists(APPPATH.'config/routes.php'))
{
include(APPPATH.'config/routes.php');
}

if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
}

if (isset($route) && is_array($route))
{
isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
unset($route['default_controller'], $route['translate_uri_dashes']);
$this->routes = $route;
}

$_c = trim($this->config->item('controller_trigger'));
if ( ! empty($_GET[$_c]))
{
$this->uri->filter_uri($_GET[$_c]);
$this->set_class($_GET[$_c]);

$_f = trim($this->config->item('function_trigger'));
if ( ! empty($_GET[$_f]))
{
$this->uri->filter_uri($_GET[$_f]);
$this->set_method($_GET[$_f]);
}

$this->uri->rsegments = array(
1 => $this->class,
2 => $this->method
);
}
else
{
if ($this->uri->uri_string !== '')
{
$this->_parse_routes();
}
else
{
$this->_set_default_controller();
}
}
}
}

Now you you have the best of both worlds, you will keep your segments with its goodies untouched but will always look for get c=&m=, i hope its clear enough.

We kept everything to default and made it check for a get request with c and m pretty like what enable_query_strings does but without enabling it and messing anything up to keep working with segments as it is..

CodeIgniter Enabling Query Strings

Query strings in 1.7.2 are a joke, it uses ?c=controller&m=method to basically change your pretty urls to psuedo $_GET params. I really can't see why anyone would use it the way it's intended, it's very misleading and is not the same as normal query strings.

I highly suggest you check out the latest version of Codeigniter, where they do not unset the $_GET array (normal query strings are now usable). In one of the core files in the older versions it says CI does not use $_GET so we are going to unset() the global $_GET array. Well, what if I need to use $GET? I always thought it was crazy, and people have been screaming for true $_GET support for forever.

Seriously though, it's time to upgrade:

Latest: https://bitbucket.org/ellislab/codeigniter-reactor/

Stable: http://codeigniter.com/

Cannot pass parameter as a query string in codeigniter

Method 01

Pass like this

<a  href="<?=base_url()?>sample_qp/new_view/<?php echo $a2 ?>">

Make sure short_url_tag is on

In Controller

public function new_view($id)
{
if(empty($id))
{
echo "Invalid Token";
}
else{
echo($id);
}
}

Method 02

<a href="<?=base_url()?>index.php?c=sample_qp&m=new_view&id=<?=$a2?>">

In application/config.php

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

In Controller

$id = $this->input->get('id');
echo $id ;

Read value from query string in CodeIgniter

Codeigniter works with URI Segments. You pass the values straight in your URL, separated with / and you grab them with positions after base_url like

$this->uri->segment(3)

Check this link: Codeigniter Documentation



Related Topics



Leave a reply



Submit