dtb81443 2013-08-27 17:33
浏览 36
已采纳

在CakePHP中使用搜索表单时的漂亮URL

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.

  • 写回答

4条回答 默认 最新

  • doutui2883 2013-08-27 18:45
    关注

    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, i.e. 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/REDIRECT/GET Pattern

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)