在路由和框架.html页面中包含GET参数

时间:2013-07-19 15:14:57

标签: cakephp cakephp-2.3 php-5.4

我有一个表格,以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

我想构建路线,以便获得以下网址:

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

但是,我如何在里面进行搜索查询?

由于

1 个答案:

答案 0 :(得分:0)

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

最简单的方法是使用服务器端URL rewriting

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

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

这会将/searches/results?search=whatever等网址重写为/search/whatever.html