Welcome Guest, Not a member yet? Register   Sign In
URI Routing
#1

[eluser]simonspoken[/eluser]
I'm trying to add search functionality to my site, but would like to avoid using javascript to rewrite the url when the form is submitted. So:

/search/?s=search+query

(instead of /search/search+query)

However, I'm having a bit of trouble with the URI routing.

I've got /search/ working fine, but would like to rewrite the results page to something like this:

$route['/search/\?s=(.*)'] = "mysite/search/$1";

This isn't working - any ideas what I'm doing wrong?

EDIT: I've just tested the above regular expression at: http://regexlib.com/RETester.aspx

It pulls out the values perfectly. Wonder why CI isn't...
#2

[eluser]simonspoken[/eluser]
Anyone got any idea why this isn't working? The regular expression is fine, but CI continues through to my "404" route because a match is not found.
#3

[eluser]xwero[/eluser]
?s=search+query is a part of the $_GET global that is destroyed by CI for security purposes so your routing rule will never be matched. Another thing is that you use a forward slash to begin your route. That is not a valid route (routing)

I don't understand why you need to have a javascript function to rewrite your url could you show how you build the url?
#4

[eluser]simonspoken[/eluser]
If I change the URL to /search/search+query, it works fine. Try putting the following code into a page:

<form><input type="text" name="s" /><input type="submit" /></form>

When you submit it, the search query will be appended as above. I can't use any fancy tricks to rewrite the url in the CI friendly format - this is being built for mobile devices.

When I knock the question mark out (so /search/=search+query), I get a message saying "The URI you submitted has disallowed characters". Could this be the problem?
#5

[eluser]xwero[/eluser]
It's a form, you should post it or is that not an option?
#6

[eluser]simonspoken[/eluser]
It's an option, but I'd rather not. If I'm unable to get it working using GET, I'll have to resort to POST.

However, all of the search engines I can think of use GET - and I'm sure there's a good reason behind that choice.
#7

[eluser]xwero[/eluser]
To bookmark the url i think. But if you want to use GET you are going to hack CI. There are some hacks on the forum to use GET without having to change all your urls to GET.
#8

[eluser]simonspoken[/eluser]
Thanks, xwero. I've switched to POST for now, until I have time to properly look through the CI code.
#9

[eluser]Pygon[/eluser]
Yes -- the majority of search engines use GET because POST cannot be url-linked.

"Hey suzy, check out these results: http://www.google.com/search/" -- wouldn't work very well.

I've never liked CI's destruction of the GET variable without providing access via Input. Have yet to find a reason it couldn't or shouldn't be done.
#10

[eluser]Michael Wales[/eluser]
Code:
function search($query = NULL) {
  if ($query === NULL) {
    // TODO: User has not performed a search, display search form
  } else {
    if (!empty($_POST['query'])) {
      // User has submitted a form with the query input
      redirect('search/'  . $query);
      return;
    }
    // If we got here, there is a query term and the user did not submit a new search
    // TODO: Query the database for search results
    $this->load->view('search/results', $this->data);
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB