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.

Answers


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.


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

Need Your Help

eacces error in RabbitMQ when trying to set up federation

macos rabbitmq fedora federation

I'm trying to prototype federated message queuing between a Mac host and a Fedora 20 image running under VirtualBox. After a certain amount of head banging I managed to get the Mac exchanges feder...

Difficulty making pgsql error message alert using jQuery AJAX

javascript jquery ajax node.js

I have a jQuery AJAX function that sends some JSON data off to be stored in my postgresql database, I am using Node, Express and node-pg.