How to make a working url like this: example.com/controller/method?id=1&cat=2

- 26,770
- 15
- 78
- 101

- 4,456
- 7
- 44
- 67
-
Is there any reason why you can't pass "id" and "cat" via URI segments? For example, example.com/controller/method/1/2 or example.com/controller/method/id/1/cat/2 ? – Colin Brock May 19 '10 at 19:23
-
maybe because this url is created not by me, what other reasons can be? – Igor Golodnitsky May 19 '10 at 21:34
-
dynback.com... we don't know. That's why we are asking. – Teej May 20 '10 at 02:59
1 Answers
It is possible by setting $config['enable_query_strings'] = TRUE;
in your config.php file (As DamienL answered). I just tried with a fresh CodeIgniter installation from here.
However, it appears there must be at least 2 variables (separated with a "&") for it to work.
Here are the steps I took to make this happen:
In config.php, I changed $config['base_url']
to the appropriate directory and set $config['enable_query_strings'] = TRUE;
In the controllers directory I created the following class:
class Testing extends Controller {
function Testing()
{
parent::Controller();
}
function index()
{
$this->load->view('welcome_message');
}
}
/* End of file testing.php */
/* Location: ./system/application/controllers/testing.php */
I can then access the index function with a query string, but only if there are 2 or more variables like this:
localhost/CodeIgniter_1.7-1.2/index.php/testing/index?id=123&cat=abc
If you absolutely need both the segment-based and query string-based approaches, but only need one variable in a particular query string, I suppose you could add a second "dummy" variable and just ignore it.

- 1
- 1

- 21,267
- 9
- 46
- 61
-
Your option is for this: http://127.0.0.1/critic/index.php?c=testing&m=index&some=1 | But I need this: http://127.0.0.1/critic/index.php/testing/index?some=1 – Igor Golodnitsky May 21 '10 at 18:17
-
@dynaback.com: Why the -1? After enabling query strings, you *can* do 127.0.0.1/critic/index.php/testing/index?some=1. I'll edit my answer to clarify – Colin Brock May 21 '10 at 18:37
-
I have 404 for this. When I set that option, only-segment and only-query-string become working. But not both in one url. – Igor Golodnitsky May 21 '10 at 19:11
-
@dynback.com: I modified my answer to provide a more precise explanation (Note the part regarding the two query string variables). Also, did my response really warrant a -1? – Colin Brock May 21 '10 at 20:24
-
yes, you really solved it. But in any case, why cant I put one query parameter ... strange – Igor Golodnitsky May 21 '10 at 21:08
-
@dynback.com: Strange indeed - I'm not sure why that is. Glad you got it working though! – Colin Brock May 21 '10 at 21:27