How to make CodeIgniter accept query string URLs?
For reliable use of query strings I've found you need to do 3 things
- In
application/config/config.php
set$config['enable_query_strings'] = true;
- Again in
application/config/config.php
set$config['uri_protocol'] = "PATH_INFO";
- 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
How to Store Birthdate and Age So That Age Can Be Updated Daily in PHP/Mysql
Basic Simplexml Working Example
Get "Content-Type" Header of Request in PHP
Make Text Wrap in a Cell with Fpdf
In PHP, How to Get the First and Last Date of a Month
SQL & PHP - Which Is Faster MySQL_Num_Rows() or 'Select Count()'
Laravel Cannot Delete or Update a Parent Row: a Foreign Key Constraint Fails
Syntax Error, Unexpected T_Encapsed_And_Whitespace, Expecting T_String or T_Variable or T_Num_String
Laravel and View Caching in Development -- Can't See Changes Right Away
Php-Sort Array Based on Another Array
How to Set for Specific Directory Open_Basedir
How to Convert an Image to Black and White in PHP
How to Convert All Images to Jpg
PHP - Listing All Directories and Sub-Directories Recursively in Drop Down Menu
Replace Ereg_Replace with Preg_Replace
PHP 5.4 Pdo Could Not Connect to MySQL 4.1+ Using the Old Insecure Authentication