Welcome Guest, Not a member yet? Register   Sign In
Using a query string inside of a clean url.
#1

[eluser]kyleect[/eluser]
I've seen a couple posts on the forum however I don't know if that's exactly the answer I'm looking for. I just need to know if this is possible:

http://localhost/controller/method/?foo=true&bar=false

The only reason I need this is for the search functionality of my app. It seems to make more sense to put the search variables in a query string rather than cram them in to a uri segment. Is CI designed to do this natively? Again, I'm sorry for posting this again but the other posts didn't seem clear enough on this issue.
#2

[eluser]Colin Williams[/eluser]
It's easily possible. Make sure these settings are in place:

Code:
$config['enable_query_strings'] = TRUE;
$config['uri_protocol'] = 'PATH_INFO'; // Anything but AUTO or QUERY_STRING
#3

[eluser]kyleect[/eluser]
Thanks!
#4

[eluser]Phil Sturgeon[/eluser]
Yup was pretty happy when I spotted this fix, until recently I always had to send search forms as a post variable to a redirect method, just to get the parameter into a CI friendly URL. Overkill!
#5

[eluser]obiron2[/eluser]
Alternatively ou could rely on javascript.

attach an onClick() event to the submit button (or any field in the form with onChange() or onDirty() ) which urlencodes the search criteria before the submit.
In CI, you then take the param1 and urlunencode it which gives you all the information you need.

This is a much better way of sending Ajax search criteria as you do not have to worry about the number of key value pairs client side and can hand off the complicated stuff to a controller (e.g. embed within the search parameter the kind of search being done and then simply call a different controller or model from your generic search controller)

You also don't have to allow GET posts, which break one of the real design niceties of CI URLs

Obiron
#6

[eluser]Colin Williams[/eluser]
I only do this when I need to interface with some outside service that uses query strings. Otherwise, using the uri_to_assoc() method usually works well in place of a query string
#7

[eluser]gh0st[/eluser]
I've done this for a simple text input search form.

I get this result;
http://ci.localhost/books/search/?phrase=my+search

But how do I get phrase back into CI?
#8

[eluser]kyleect[/eluser]
$_GET['phrase']
#9

[eluser]Colin Williams[/eluser]
do http://ci.localhost/books/search/phrase/my+search instead
#10

[eluser]gh0st[/eluser]
Quote:do http://ci.localhost/books/search/phrase/my+search instead

Whilst that's fine for a small form, but the form I'm building it for has many variables.

So it'd be something like this;
Quote:http://ci.localhost/books/search/book/my...rch/...etc

That means I'd have to know which of the URI parameters to grab.

Also it still doesn't stop the + appearing in the my+search

I'm quite happy to use:
http://ci.localhost/books/search/?phrase...alue...etc..

Until there is a clear cut way of doing this.




Theme © iAndrew 2016 - Forum software by © MyBB