How to Enable Query String in CodeIgniter

On 11/07/2016

CodeIgniter Query String Example: This article shows you how to enable query string in codeigniter. PHP Codeigniter framework uses segment based approach for its URLs, thus making them Search Engine friendly. But those who want to use the good old query string method can turn on this option in code igniter and use it. In case you don't know, here's how to enable query string in codeigniter.

CI follows the default URL format similar to this,

mydomain.com/index.php/my_controller/my_function/true

Above url consists of three URI segments where,

  • my_controller defines name of the controller
  • my_function represents the function name
  • true refers to the parameter passed to the function

If you think index.php makes the URL ugly, then you can easily remove index.php from the url by rewriting them in htaccess file. It gives you cleaner URL like this,

mydomain.com/my_controller/my_function/true
enable-query-string-in-codeigniter

Well! But what if you want to take the traditional query string approach for urls like this?

index.php?c=products&m=show&id=25

There are situations especially when working with APIs, some of them forces you to use query strings in the app. Don't worry! Codeignitor allows you to use query strings for urls by choice. But before working with query strings you have to enable them in codeigniter.

How to Enable Query String in CodeIgniter?

Step-1) Go to 'application/config.php' file and open it.

Step-2) Now look for these settings,

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

Step-3) Change the 'enable_query_strings' setting to TRUE.

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

Usage:

<?php
class products extends CI_Controller
{
    ...            

    function show($id = NULL)
    {
        ...
    }
}
?>

You can access this page with the url,

index.php?c=products&m=show&id=25

Must Read: How to Upload Multiple Images/Files in CodeIgniter

Don't Miss: Retrieve Data From Database with CodeIgniter & AJAX

Likewise you can enable query string in codeigniter and use them in your applications.

1 comment:

  1. I think you're all tutorials are copy from other sites, please post your own contents if you have.

    ReplyDelete

Contact Form

Name

Email *

Message *