0

I have a search form, which uses a Search controller/model.

echo $this->Form->create('Search', array('action' => 'query', 'type' => 'get'));
 ...
echo $this->Form->end();

But by default the form submits to '/searches/query'. How do I get the URL of the search page to be /search/query instead?

I don't really want to use .htaccess rewrites if possible, as that seems kind of messy. Hoping there is a tidy Cake way of doing this.

I think this could be done with a custom Inflector rule in bootstrap.php maybe, but I'm not sure how.

BadHorsie
  • 14,135
  • 30
  • 117
  • 191

2 Answers2

2

Just use the router. In your routes file, add:

Router::connect('/search/:action/*', array('controller' => 'searches'));
Router::connect('/search/*', array('controller' => 'searches', 'action' => 'index'));

Read more about the router in the book.

jeremyharris
  • 7,884
  • 22
  • 31
  • This will make a two redirection and somehow will increase time execution. Isn't it? – Arun Jain Jul 18 '12 at 04:53
  • 3
    No, this is not a redirection. This is [generic routing](http://book.cakephp.org/2.0/en/development/routing.html) not redirects will happen. What this will do is that in will instruct the CakePHP core to call the `SearchesController` when you request the following URL: `www.myapp.com/search/query`. – Borislav Sabev Jul 18 '12 at 05:18
  • Sorry, this is not the answer, even though everyone is upvoting you. I know how to use routes and I already have this route set up. It's fine if the user types `/search/index` in the address bar and goes there themself, but it doesn't solve the problem that my form still submits to `/searches/query` – BadHorsie Jul 19 '12 at 10:07
  • The form should submit to `/search/query`. If it's not, then something else is wrong. This is the correct way to do it. Show your entire routes file and a snippet of the controller with the action. – jeremyharris Jul 19 '12 at 14:01
0

Isn't there a way to say:

echo $this->Form->create('Search', array('action' => 'search/query', 'type' => 'get'));

And then setting up a router for this?

$this->Router->('search/query', array('controller' => 'searches', 'action' => 'query'));
Pascal Precht
  • 8,803
  • 7
  • 41
  • 53
  • Yes, that was the first thing I tried but the action will only be the second part. You can't seem to specify the controller to use or the full URL. So it comes out in the form as `action="/searches/search/query"` – BadHorsie Jul 17 '12 at 14:30