Snehalk
Snehalk

Reputation: 61

codeigniter url routing with query string

I have 2 url in my project

http://example.com/one.html

http://example.com/one.html?param1=value&param2=value

In route file I have rules as below

$route['search/one?(:any)'] = 'advance_search/books';
$route['search/one'] = 'advance_search/index';

when I call 2nd url with query string it still call index method. any one help to fix what is wrong.

Upvotes: 1

Views: 4338

Answers (2)

Ellert van Koperen
Ellert van Koperen

Reputation: 583

I was struggling with something similar, and found that parsing the URL myself offers the most felixibility.

public function search() {
    $params = array();
    $url = parse_url($_SERVER['REQUEST_URI']);
    parse_str($url['query'], $params);
    // $params now is an array containing the name-value pairs of the querystring. 
    echo("<pre>".print_r($params ,true)."</pre>\n");exit; // for debugging, 
}

This went together with the route

$route['search/(.+)'] = 'search/search/$1';

This dumps all parameters into the search function of the search controller and allows you to handle it as you see fit.

Upvotes: 0

user4419336
user4419336

Reputation:

Try with your route like

http://example.com/index.php/controller_name?param1=value&param2=value

Controller has to be a php file

Make sure the first letter only is upper case on class and file name.

controllers/One.php

<?php

class One extends CI_Controller {

    public function index() {

      echo $this->input->get('param1');


    }
}

If you need to remove index.php try some of these htaccess

https://github.com/wolfgang1983/htaccess_for_codeigniter

Upvotes: 1

Related Questions