5

According to CI's docs, CodeIgniter uses a segment-based approach, for example:

example.com/my/group

If I want to find a specific group (id=5), I can visit

example.com/my/group/5

And in the controller, define

function group($id='') {
    ...
    }

Now I want to use the traditional approach, which CI calls "query string" URL. Example:

example.com/my/group?id=5

If I go to this URL directly, I get a 404 page not found. So how can I enable this?

zihaoyu
  • 5,483
  • 11
  • 42
  • 46

10 Answers10

10

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]
WeeJames
  • 540
  • 4
  • 11
  • I still get a 404 error. CodeIgniter is located at `my.server.com/project/ci`, and there is `my.server.com/project/ci/index.php`, `my.server.com/project/ci/.htaccess`, and `my.server.com/project/ci/system/application`. – zihaoyu May 24 '10 at 17:33
  • 1
    My bad. I've just realised what youve asked. You cannot mix and match the query string / segment approach. If you want to use segments to access the controller/method you need to pass in the id via that method as well `example.com/my/group/5/`. Or you need to include something like `$id = $this->input->get('id)';` at the top of your method if you really want to get it out of the query string. Or you can enable query strings and use that to select the controller and method as well `my.server.com/project/ci/index.php?c=my&m=group&id=5`. – WeeJames May 25 '10 at 07:14
  • Thanks, I put `$id=$this->input->get('id')` at the beginning of my controller, and set `$config['uri_protocol'] = "QUERY_STRING";` in config.php. But `example.com/my/group?id=5` still does not work. I got the 404 error. What else should be done? – zihaoyu May 27 '10 at 16:56
  • YOu need to modify the `application/config/config.php` line `$config['enable_query_strings'] = FALSE;` to be `true`. Leave the uri_protocol at `auto`. – WeeJames Sep 08 '10 at 10:14
6
//Add this method to your (base) controller :
protected function getQueryStringParams() {
    parse_str($_SERVER['QUERY_STRING'], $params);
    return $params;
}


// Example : instagram callback action
public function callback()
{
    $params = $this->getQueryStringParams();
    $code = !empty($params['code']) ? $params['code'] : '';

    if (!empty($code))
    {
        $auth_response = $this->instagram_api->authorize($code);

        // ....  
    }

    // .... handle error
}    
Nassif Bourguig
  • 755
  • 1
  • 10
  • 10
  • Great! This is a perfect solution of you want to limit the use of query strings to only those api's that force you to use them. – bottleboot May 21 '13 at 09:33
  • This is actually the less invasive and working solution, you get all the segments on one side and the query strings via the function. Excellent. Still good for latest CI 3.X – cdsaenz May 15 '20 at 16:03
1

You may change URI PROTOCOL in your config file to

  $config['uri_protocol']   = "ORIG_PATH_INFO"; 

and

  $config['enable_query_strings'] = FALSE;

It'll accept query strings and allow your URLs. Worked for me :)

sandip
  • 3,279
  • 5
  • 31
  • 54
  • For me, it was a combination of this, and the Rewrite Rule settings in .htaccess from other answers. Hope that helps someone else. – Evan May 09 '19 at 14:59
1

Html:

<a href="?accept=1" class="btn btn-sm btn-success">Accept</a>

Controller Function

if ($this->input->get('accept')!='')
{
    $id = $this->input->get('accept', TRUE );
    $this->camprequests->accept($id);
    redirect('controllername/functionname');
}

Model Function

public function accept($id)
{
    $data = array('status'=>'1');
    $this->db->where('id','1');

    if($this->db->update('tablename',$data)) {

        $this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Accpeted successfully.</div>"); 

    } else { 

        $this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Error..</div>");  
    }
}
PHP Ninja
  • 1,055
  • 2
  • 9
  • 28
1

This might help some people; put this into your controller's constructor to repopulate $_GET on a controller-by-controller basis (e.g. if you are integrating a third party lib that relies on $_GET - such as most PHP OAuth libraries).

parse_str(str_replace($_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']),$_GET);
Leo
  • 37,640
  • 8
  • 75
  • 100
  • I tried it with parse_str($_SERVER['QUERY_STRING'],$_GET); which didn't work. But this one does it for me. Thank you! – mseo Jan 06 '12 at 01:58
0

CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you'll see these items:

enter code here $config['enable_query_strings'] = FALSE;

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

If you change "enable_query_strings" to TRUE this feature will become active.

GkDreamz
  • 51
  • 9
  • Please quote your sources when copy&pasting. http://codeigniter.com/user_guide/general/urls.html – Pekka Jul 05 '12 at 09:01
0

This is actually tested and confirmed

It works with any method you like; giving you freedom to mix match the query string and / segment approach (as opposed to the previous responses)

either you want to use:

example.com/my/group/?id=5

(please note the trailing / before ?). OR

 example.com/my/group/5 

(depending on your url pattern definitions in the router file). OR EVEN

example.com/index.php/?my/group/?id=5

(though that looks awkward enough)

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

and in you codigniter's config/config.php file, set

$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = TRUE;
tormuto
  • 587
  • 5
  • 16
0

Modify application/config.php at the line:

$config['enable_query_strings'] = FALSE;

Make this true instead. There are other details you'll have to pay attention to also. See here.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • That configuration appears to instruct the URL interpreter to use the query string instead of path segments for its controller/method arguments. I'm not really sure that's what the questioner wants. – eyelidlessness May 24 '10 at 01:18
  • I've reread the question and the question appears to be "how to enable query string URLs? – wallyk May 24 '10 at 01:21
  • I agree with eyelidlessness. This setting only allows you to use controller and method parameters in the query string URL. – zihaoyu May 24 '10 at 01:21
0

After setting $config['enable_query_strings'] = TRUE; in your config.php file, you can use the segment-based approach in conjunction with query strings, but only if you use 2 or more variables (separated by a "&") in the query string like this:

example.com/my/group?id=5&var=something

See this answer for more information.

Community
  • 1
  • 1
Colin Brock
  • 21,267
  • 9
  • 46
  • 61
  • Are you saying that if I only have one param (id), then I cannot use query string URL? – zihaoyu May 24 '10 at 17:34
  • @Peter: If you want the controller and methods as URI parameters (rather than variables in the query string), then yes - based on some brief testing it appears you need to append 2 or more variables to the query string (I don't know why). If only one variable is present, it throws a 404 error. – Colin Brock May 24 '10 at 19:15
0

I have tried these steps and it was correct for me to set a query in pagination (codeigniter 3)

in controller: just add these codes.

$config['enable_query_strings'] = TRUE;

$config['page_query_string'] = TRUE;

$config['query_string_segment'] = 'page'; // you can jot down what you want instead of page in this one. and you could get by this name your $_GET method. So, if you don't use this line, it will automatically create a query with "per_page" and this will be your query. 

related link: https://codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination

Even, if you want to add your own specific class to the (a tags that are created by pagination), you must add the following code to have your own favorite style for pagination links.

$config['attributes'] = array('class' => 'page-link'); // this is the example of the above and you can write what you want instead of 'page-link' as the value of class.

This will be very useful when we want to add page-link class as boostrap style to the a tag.

result: <a href="home?action=questions&amp;page=12" class="page-link" data-ci-pagination-page="5" dideo-checked="true">5</a>

as you see page-link was added to this a tag.

related link: https://codeigniter.com/userguide3/libraries/pagination.html#adding-attributes-to-anchors

Pouria
  • 1
  • 2