views:

313

answers:

1

Hey!

I have to rewrite a form in a CakePHP application. The form searches a list of car sale ads and is pretty complex (~10 fields: model, manufacturer, fuel type etc.). Currently, the form is sent via POST and is working quite well. The problem is I need to create bookmarkable URLs from the search queries and I was wondering what was the best way to accomplish this.

I tried passing 'type' => 'get' to the $form->create() method, but this generates a URL like usercars/search/?model=...&[...] and causes the search to break.

I want the URL to be something like <some routing>/<Manufacturer>/<Model>/<Fuel>/<etc>, and also to support parameter omission (i.e. <some routing>/<Manufacturer> to list all the cars from the given manufacturer, even if the other parameters are missing). By POST, currently this works. If I select only one of the params, it gives me the correct results.

I wonder if there's any way to determine cake to build the nice url from the form submission and maintain compatibility with the $this->data way of handling input in the controller. I've gone through the documentation and haven't found any way to do this.

I already have two alternatives:

  • rewriting the controller action to support function parameters instead of working with $this->data, building a route that targets the search method with the params and doing a redirect in the controller's beforeFilter() method to a URL that corresponds to the route.
  • doing javascript on the search form page to build the URL + the above way of handling it.

What would you recommend?

+1  A: 

There is a built in way to do this via cake's named arguments. So if you structured your links to look like

/controller/action/manufacturer:toyota/model:prius/

In your controller action, they would be available in $this->passedArgs as an associative array that looked like this

Array
(
    [manufacturer] => toyota
    [make] => prius
)

See also:

http://book.cakephp.org/view/46/Routes-Configuration

Here is an example of using prototype to dynamically create the form url on submit (using prototype).

Event.observe(window, 'dom:loaded', function(e1)
{
    $("formid").observe('submit', function(e)
    {
        this.select(".inputclass").each(function(input)
        {
           name = input.readAttribute('name')
           val = name.match(/data\[.*\]\[(.*)\]/);

           if(val && val[1])
           {
               name = val[1]
           }

           $("formid").action += "/" + name  + ":" + $F(input);
        });

        return this.submit();
    });   
});
JoeyP
Thanks for the answer ;). Do you have any idea how I can get the form to submit to _that_ url?
Alex Ciminian
You have to submit the form in the usual way. And then in the controller action you have to "translate" the submitted data to a friendly url and redirect to it.
dhofstet
Yep, you could do it how dhofster is suggesting. There is a bakery article that describes this method here:http://bakery.cakephp.org/articles/view/form-posting-with-_get-parametersI'd recommend using the component if you were to go that route. If you would like to avoid doing a redirect on every form submit, you could use javascript to dynamically create the action url on form submit. I have added a javascript (prototype) example to the original answer.
JoeyP