0

I have a form which submits data over as GET

Which means, when I do:

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();
     ?>

I get the URL as : example.com/Searches/results?search=asdadasdasd

I want to frame routes such that I get the following URL:

example.com/search/asdadasdasd.html

I had a look at : http://book.cakephp.org/2.0/en/development/routing.html

I got how to get an extension : http://book.cakephp.org/2.0/en/development/routing.html#file-extensions

But, how will I get search query inside?

Thanks

Keval Domadia
  • 4,768
  • 1
  • 37
  • 64

1 Answers1

0

That's not possible with CakePHP itself, when generating the form it doesn't know about the search term since it doesn't exist yet, so generating such a URL would have to be done dynamically when submitting the form, ie on the user side, which would require JavaScript.

The easiest thing to do would be to use server-side URL rewriting.

Here's an example (should work fine in app/webroot/.htaccess), it tests the query string for the search key, and uses its value for a redirect in case the path of the URL matches /searches/results (both, path and query string are treated case-insensitive):

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

This would rewrite urls like /searches/results?search=whatever to /search/whatever.html.

ndm
  • 59,784
  • 9
  • 71
  • 110