带有友好URL的CakePHP表单提交

带有友好URL的CakePHP表单提交

问题描述:

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?

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();
    });   
});