0

我有一个以 GET 形式提交数据的表单

这意味着,当我这样做时:

echo $this->Form->create('Search', array('type'=>'get', 'url'=> array('controller'=>'searches','action'=>'results')));
echo '<div class="search-box">';
echo $this->Form->input('search', array(
    'class' => 'search-input',
    'placeholder' => 'Search Domain Names',
    'label'=>false));
echo $this->Form->input('', array(
    'class' => 'search-icon',
    'label' => false,
    'type' => 'submit'
    ));
echo $this->Form->end();
     ?>

我得到的网址为:example.com/Searches/results?search=asdadasdasd

我想构建路由,以便获得以下 URL:

example.com/search/asdadasdasd.html

我看了看:http ://book.cakephp.org/2.0/en/development/routing.html

我知道如何获得扩展名:http: //book.cakephp.org/2.0/en/development/routing.html#file-extensions

但是,我将如何在里面获取搜索查询?

谢谢

4

1 回答 1

0

这对于 CakePHP 本身是不可能的,当生成表单时它不知道搜索词,因为它还不存在,所以生成这样的 URL 必须在提交表单时动态完成,即在用户端,这将需要 JavaScript。

最简单的做法是使用服务器端URL 重写

这是一个示例(在 中应该可以正常工作app/webroot/.htaccess),它测试search键的查询字符串,并在 URL 的路径匹配时使用其值进行重定向/searches/results(路径和查询字符串都被视为不区分大小写):

RewriteCond %{QUERY_STRING} ^search=(.*)$ [NC]
RewriteRule ^searches/results$ /search/%1.html? [NC,R=302,L]

这将重写 url 之类/searches/results?search=whatever/search/whatever.html

于 2013-07-19T21:02:10.400 回答