BadHorsie
BadHorsie

Reputation: 14544

CakePHP - Best way to customise form action?

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.

Upvotes: 0

Views: 2196

Answers (2)

jeremyharris
jeremyharris

Reputation: 7882

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.

Upvotes: 2

Pascal Precht
Pascal Precht

Reputation: 8893

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'));

Upvotes: 0

Related Questions