cdub
cdub

Reputation: 25701

CakePHP Form Submission with Results in Url

How do I submit a form using the form helper and have the reply of that submission have a url with what was searched for?

I submit this code:

 <?php echo $form->create('Search', array('action' => 'results', 'type' => 'post')); ?>
    <?php

            $options = array
            (
                'size' => 45,
                'id' => 'search',
                'tabindex' => 1,
                'maxlength' => 250
            );

            echo $form->text('Search.query', $options);
        ?>

So when I submit the form with the words "Hello World", I want the resulting url to be:

 [domain]/searches/results/Hello+World

Upvotes: 1

Views: 787

Answers (1)

deceze
deceze

Reputation: 522024

You will have to do a redirect to get this exact URL. Submitting a form using GET would result in /searches/results?SearchQuery=Hello+World. For my taste that would be perfectly adequate, but if you want a pretty URL, do this in your controller:

class SearchesController extends AppController {
    public function results($query = null) {
        if (!$query && $this->data) {
            $this->redirect(array('action' => 'searches', $this->data['Search']['query']));
        }

        // search
     }
}

Note that this requires one extra roundtrip to the server.

Upvotes: 2

Related Questions