Cakephp: How to make form get variables show up as passed args?
I have an action called search on a controller called categories. /categories/search
I also have this route in place:
Router::connect(
'/search',
array('controller'=>'categories','action'=>'search')
);
So the URL is /search
instead of /categories/search
I have a form set to get
for that url:
$form->create(
NULL,
array(
'type'=>'get',
'url'=>array('controller'=>'cate开发者_运维百科gories','action'=>'search')
)
);
This form contains 1 input field named q
and when you submit it the URL you are taken to looks just like this:
/search?q=your+search+terms
The problem is this would play much nicer with other parts of the application if it were a passedArg
instead of a get var. So the URL would look like this:
/search/q:your%20search%20terms
Is there anyway to set the form to post like this?
first, the router:
Router::connect('/search', array('controller'=>'categories','action'=>'search')); Router::connect('/search/*', array('controller'=>'categories','action'=>'search'));
You can use redirect to have the pretty url:
function search(){ if(!empty($this->params['url']['q'])){ $this->redirect(array('q'=>$this->params['url']['q'])); }else if(!empty($this->params['named']){ // search here } }
This will never work without a JS hack because named parameters are part of the URL. The form's target url is static, and won't be modified by the contents of an input field. You can do this with a JS hack though, but isn't very nice.
精彩评论