Codeigniter, by default crafts it’s URL in a search engine friendly format. It uses a segment-based approach and does away with the all familiar querystring format that developers have been using for many years to pass parameters via the URL into their applications.
Codeigniter does allow you to turn on the querystring capability, but that would mean you have to use a pure querystring approach, foregoing the segment-based approached.
So, is it possible to mix segments and querystring?
Thankfully, Codeigniter can be configured to mix the segments and querystring, and all it takes are some tweaks in your
application/config.php
file and perhaps an additional line of code.Say you would like to have a URL like so:
example.com/product/search/?pname=test&pid=2
example.com/product/search/test/2
In most cases, this is not really a show stopper. But there are times when you have no choice but to include querystrings into your URL. For example, a legacy application that interfaces with your application perhaps?
If you follow the (excellent) Codeigniter user guide, and you turn on the querystring capability , Codeigniter will now function with a URL like this:
example.com/?c=product&m=search&pname=test&pid=2
This is what you can do if you need to have a mixed segment and querystring approach. You can enable this either globally for your whole application or just locally within a controller.
Option 1: Mixing Globally
To enable this globally, go to your
application/config.php
file, and look for $config['uri_protocol']
variable and change it to:$config['uri_protocol'] = "PATH_INFO";
$config['enable_query_strings']
variable and change it to:$config['enable_query_strings'] = TRUE;
Option 2: Mixing Locally
If you don’t want to enable this capability across your whole application, you can restrict it to only the controllers you want.
You can do this by going to the
application/config.php
file and switching $config['enable_query_strings']
back to ‘FALSE’.$config['enable_query_strings'] = FALSE;
$config['uri_protocol']
as PATH_INFO$config['uri_protocol'] = "PATH_INFO";
parse_str($_SERVER['QUERY_STRING'],$_GET);
No comments:
Post a Comment