dongshijiao6890 2010-04-04 14:12 采纳率: 0%
浏览 30
已采纳

带有友好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?

  • 写回答

1条回答 默认 最新

  • douliang6563 2010-04-05 20:43
    关注

    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();
        });   
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog