CakePHP - Best way to customise form action? - forms

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.

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

Related

Cakephp2 post to plugin controller

Having hard time figuring out how to post a form to a plugin controller.
Say I have a reg/login plugin that could be shared with different apps.
in reg.ctp:
echo $this->Form->create(null, array('url'=>array('controller' => 'user', 'action' => 'submit','plugin'=>'user')));
I get missing controller.. in app/controller/user.php
what did do wrong?
Looks like your controller is not pluralised. All controllers in Cake, by convention, are plurals.
array('controller' => 'users', 'action' => 'submit', 'plugin' => 'user');
Should route to
app/Plugin/User/Controller/UsersController::submit()

CakePHP Text as Form Submit

I've searched the web and have come up with nothing. (Multiple search engines too - I have looked!)
I'm trying to have a text link as the 'form submit' button. Any ideas if this is possible in CakePHP?
Current view code below!
<?php
echo $this->Form->create('trainees', array(
'action' => 'reassign'
));
echo $this->Form->input('emailaddress', array(
'value' => 'scott#something',
'type' => 'hidden',
));
echo $this->Form->submit('Re-Assign Mentor', array(
'class' => 'submit mid',
'before' => '<p>',
'after' => '</p>'
));
echo $this->Form->end();
?>
You need to use the HtmlHelper to output a link. In it's simplest form you use the text you want displayed with the URL that it should link to. In this case it will be JavaScript:
$this->Html->link('Submit Form', 'javascript:document.forms["myform"].submit();');
There are two additional parameters (a $options array and $confirmMessage boolean), but they along with the URL are optional.
You can also call your own JavaScript function if you need to do client side verification and call the submit function from there (also verify on the server as clients can lie).
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link

Pretty Zend Framework urls

I would like to have my urls like this:
/index
/contact
/articles
/articles/selection
...
Instead of:
/index/index
/index/contact
/articles/index
/articles/selection
...
Basically I have only one controller. Which solution is the best to perform this? (controllers and redirections, ZF routing, url rewriting, something else?)
Have a look at the documentation. The behaviour you want is configured as default in the default router:
http://framework.zend.com/manual/en/zend.controller.router.html
if the first param do not maps a module name, it will search for a controller and if this fails too, it is looking for an action in your IndexController.
Did you tried calling your url's like you want to?
What happens if you navigate to /index? Should be the same like /index/index
use zend routing :
$router = Zend_Controller_Front::getInstance()->getRouter();
$route_index = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('route_index', $route_index );
$route_articles = new Zend_Controller_Router_Route('articles/:action', array(
'module' => 'default',
'controller' => 'articles',
'action' => 'index'
));
$router->addRoute('route_articles ', $route_articles );

Redirection from module to application

how can we go back form one module to parent application in ZEND. I use this code:-
<?php echo $this->url(array('controller' => '', 'action' => '')); ?>
If you add a 'module' key to the URL array, you can easily traverse modules. If you're trying to get to the "Parent" (the default module, perhaps?), you'd change your code to:
<?php echo $this->url(array('module' => 'default', 'controller' => 'index', 'action' => 'index)); ?>

Zend Framework Change parameter in route on the same spot?

I'm not sure how to fix this, or wat is the best way to approach this. Also couldn't find enough information to get me on the right way (could be that my searching sucks..)
Anyway, this is my problem:
I defined a route in my bootstrap file:
protected function _initRoutes()
{
$router = $this->frontController->getRouter();
$router->removeDefaultRoutes();
$router->addRoute(
'delete',
new Zend_Controller_Router_Route('/:controller/:action/:id/',
array('controller' => ':controller',
'action' => ':action',
'id' => ':id',
)
)
);
}
This works perfectly for my update and delete actions.
Now I've added the pagination to the indexpage. The pagination expects the page parameter. Because I haven't set this in my route, it cannot pass it, so my pagination doesn't work (as in switching between results).
I understand this. But what I want is that on the index page the id parameter isn't necessary and replace this with the page parameter.
Trying another route replacing id with page didn't work.
Is there a good way to solve this in the bootstrap or is it the best way to check for the action, and depending on the action, index or update/delete, define the route. The best place would than be a plugin?
Any advice or tips are greatly appreciated!
While working on another aspect of the application I came back to the same problem. I solved it, by specifying the routes much more.
First I deleted the $router->removeDefaultRoutes(); rule.
And then instead of (which didn't work):
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/:action/:page', array('controller' => ':controller', 'action' => ':action', 'page' => ':page'))
);
I now use this:
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/index/:page', array('controller' => ':controller', 'action' => 'index', 'page' => ':page'))
);