简体   繁体   中英

Pretty URLs when using search form in CakePHP

At the moment I have a basic search form set up and working. This is in my view:

<form action="/search" method="get" id="search">
    <input type="text" name="q" placeholder="Enter search terms..."><input type="submit" value="Search">
</form>

And my action simply executes the following:

$this->set('recipes', $this->paginate(array(
    'Recipe.name LIKE' => '%' . $this->request->query('q') . '%'
)));

This results in URLs like http://mysite.com/search?q=querystring . While this is all well and good, I'd like to have the URL as something along the lines of http://mysite.com/search/querystring .

Is there a way to achieve this which doesn't involve redirecting the user after they submit the form? If that's the only way I'd rather stick with using the $_GET parameter.

Use routing http://book.cakephp.org/2.0/en/development/routing.html for pretty URLs. The downside of the other answer you've got with .htaccess is that this won't work in both direction. The URL generated by CakePHP won't be a pretty URL because it's php that generates it.

What you try to implement is a pattern known as PRG . The CakeDC search plugin implements that pattern and makes it easy to use it.

You can use it and just add the routes you need to get your pretty URL.

You can set an .HTACCESS file to use a RewriteRule.

Make a file called .htaccess (nothing before the dot) and inside put the following contents:

RewriteEngine On
RewriteBase /
RewriteRule ^search/(.*)$ search.php?query=$1 [L]

The first two lines configure the engine and the third redirects /search/* to /search.php?query=* using regexp. PHP will never know anything about this, it's all done by Apache.

I would do this using javascript/jquery. I'm thinking something like this, though I haven't tested this or anything.

$('#form').submit(function(e) {
    e.preventdefault();
    window.location.href = "http://mysite.com/search/" + encodeURIComponent($('#search').val());
});

A much better approach for this would be a redirect after a POST. Why? Here are the four top reasons:

  1. It will allow your users to bookmark searches much easier (read on for reasons)
  2. The resulting url is much more readable, ie what you want to achieve
  3. easier caching for search-results
  4. reloads are much faster (if results are cached)

Want to know how to achieve it? Read on...

  1. If you hit a submit on a form it will be a post to a specific location. That location uses the form-data to identify the searchterm. It will then do a http redirect to a second url
  2. the second url will be according to whatever your requirement is.

have a look at this, it makes things more clear.

POST /重定向/获取模式

source: http://www.phphatesme.com/blog/webentwicklung/postredirectget-pattern/ - good german article on this subject. Sorry for hotlinking the image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM